Click to See Complete Forum and Search --> : problem with inheritance
grubera
10-06-2006, 09:07 AM
hello!
I use inheritance. so normally, I should be able to use the variables defined in the mother class without any problems and without creating an object of this class. but somehow, this is not possible. only when I write 'this->' in front of every variable. because I have to do this 1000 times, I wonder, if there would be another possibility!
I read something about 'using namespace blabla', but I couldn't figure out how to use this...
I use gcc 3.4.6
any suggestions or ideas?
thank you in advance,
alex
drkybelk
10-06-2006, 09:36 AM
Hi,
it would be interesting to see your code. Yes, you should be able to to see all the the public and protected members of the parent. Good design would be to make the member variables of your classes private, but if you fail to do so, yes, they should be visible as well.
BUT: only static members (variables and methods) can be accessed without having an instance of a class. Could you please clarify? As said before:- code would be helpful...
Cheers,
Dieter
grubera
10-06-2006, 12:22 PM
hi!
thanks for your reply. unfortunately, it's already weekend here @ our place, so I will send you the code monday morning!
have a nice weekend!
bye alex
Danny
10-07-2006, 03:31 PM
you certainbly don't need this->member to access a public member of a base class in a derived class, unless the same name is redeclared in the derived class. However, it will be much easier to understand the problem and solve it if you show us the relevant code.
grubera
10-10-2006, 04:53 AM
hello!
the code I try to compile is a disco system for running on a Solaris 2.10 x86 system...
I use the compiler gcc with version 3.4.6 and get the error message:
bash-3.00# gmake prod
gmake[1]: Entering directory .../MESSAGE/src'
Compiling C++ file MSG_Processor.cpp ...
#@g++ -W -Wpointer-arith -pipe -Wno-uninitialized -D_POSIX_THREADS -D_POSIX_THREAD_SAFE_FUNCTIONS -D_REENTRANT -I/usr/local/SMCgcc/reloc/include/g++-3/ -D_SUNOS -fPIC
-I/.../Trace/inc -I/alexander/dss/disco/src/MESSAGE/inc
-I/.../MESSAGE/src -I/alexander/dss/disco/src/MAIN/inc
-I/.../SHARED/inc -c MSG_Processor.cpp -o .SunOS-5.10_/MSG_Processor.o
g++ -W -Wpointer-arith -pipe -Wno-uninitialized -D_POSIX_THREADS -D_POSIX_THREAD_SAFE_FUNCTIONS -D_REENTRANT -I/usr/local/SMCgcc/reloc/include/g++-3/ -D_SUNOS -fPIC
-I/.../Trace/inc -I/alexander/dss/disco/src/MESSAGE/inc
-I/.../MESSAGE/src -I/alexander/dss/disco/src/MAIN/inc
-I/.../SHARED/inc -c MSG_Processor.cpp -o .SunOS-5.10_/MSG_Processor.o
In file included from /.../MESSAGE/inc/MSG_TemplateData.hpp:153,
from MSG_Processor.cpp:12:
/.../MSG_TemplateData.cpp: In member function `virtual unsigned int MSG_SimpleData<T>::exportData(char*)':
/.../MESSAGE/src/MSG_TemplateData.cpp:73: error: `_convert' was not declared in this scope
so first: content of MSG_Data.hpp: (only the important parts!)
class MSG_Data
{
public:
MSG_Data (const DSC_DataInfo_struct& datum, bool doConversion);
virtual unsigned exportData (char* buffer) = 0;
virtual unsigned importData (const char* buffer) = 0;
inline unsigned maximumSize () {
return _maximumSize;
}
protected :
const char* _name;
DSC_uint32_t _maxOccurrences;
DSC_uint32_t* _nbOccurrencesPtr;
unsigned _nbOctets;
unsigned _maximumSize;
void* _ptr;
bool _variableSize;
bool _simpleData;
DSC_DataType_enum _type;
unsigned _typeSize;
unsigned _dimension;
bool _convert;
};
then the content of MSG_Data.cpp: (only the important parts!)
#include "DSC_Config.h"
#include "MSG_Data.hpp"
MSG_Data::MSG_Data (const DSC_DataInfo_struct& datum, bool doConversion) :
_convert(doConversion),
_name(datum.name),
_dimension(datum.dim),
_type(datum.type),
_ptr(datum.ptr),
_maxOccurrences(datum.maxOccurrences),
_nbOccurrencesPtr(datum.occurrencesPtr)
{
_simpleData = (_dimension > 1) ? false : true;
_variableSize = (datum.variableSize==1);
}
MSG_Data::~MSG_Data()
{
/* Nothing */
}
then the content of MSG_TemplateData.hpp: (only the important parts!)
#ifndef _MSG_TEMPLATEDATA_HPP_
#define _MSG_TEMPLATEDATA_HPP_
#include "DSC_Config.h"
#include "DSC_Types.h"
#include "DSC_Tools.hpp"
#include "DSC_OS.hpp"
#include "MSG_Types.hpp"
#include "MSG_Data.hpp"
#include "DSC_AdvancedAPI.h"
#include "DSC_IntTypes.h"
#include "DSC_FloatTypes.h"
#include __DSC_STRING
template <class T>
class MSG_TemplateData : public MSG_Data
{
public:
MSG_TemplateData (const DSC_DataInfo_struct& datum, bool doConversion);
template <class T>
class MSG_SimpleData : public MSG_TemplateData<T>
{
public:
MSG_SimpleData (const DSC_DataInfo_struct& datum, bool doConversion) :
MSG_TemplateData<T> (datum,doConversion) {}
~MSG_SimpleData() {}
virtual unsigned exportData (char* buffer);
virtual unsigned importData (const char* buffer);
};
typedef MSG_SimpleData<DSC_uint8_t> MSG_SimpleDataU8;
typedef MSG_SimpleData<DSC_uint16_t> MSG_SimpleDataU16;
typedef MSG_SimpleData<DSC_uint32_t> MSG_SimpleDataU32;
typedef MSG_SimpleData<DSC_uint64_t> MSG_SimpleDataU64;
typedef MSG_ArrayData<DSC_uint8_t> MSG_ArrayDataU8;
typedef MSG_ArrayData<DSC_uint16_t> MSG_ArrayDataU16;
typedef MSG_ArrayData<DSC_uint32_t> MSG_ArrayDataU32;
typedef MSG_ArrayData<DSC_uint64_t> MSG_ArrayDataU64;
typedef MSG_SimpleDataVarSize<DSC_uint8_t> MSG_SimpleDataVarSizeU8;
typedef MSG_SimpleDataVarSize<DSC_uint16_t> MSG_SimpleDataVarSizeU16;
typedef MSG_SimpleDataVarSize<DSC_uint32_t> MSG_SimpleDataVarSizeU32;
typedef MSG_SimpleDataVarSize<DSC_uint64_t> MSG_SimpleDataVarSizeU64;
typedef MSG_ArrayDataVarSize<DSC_uint8_t> MSG_ArrayDataVarSizeU8;
typedef MSG_ArrayDataVarSize<DSC_uint16_t> MSG_ArrayDataVarSizeU16;
typedef MSG_ArrayDataVarSize<DSC_uint32_t> MSG_ArrayDataVarSizeU32;
typedef MSG_ArrayDataVarSize<DSC_uint64_t> MSG_ArrayDataVarSizeU64;
#include "MSG_TemplateData.cpp"
#endif // _MSG_TEMPLATEDATA_HPP_
and finally the code which produces the error in MSG_TemplateData.cpp:
...
#include "DSC_Config.h"
#include "MSG_Trace.hpp"
#include "MSG_TemplateData.hpp"
...
template<class T>
unsigned MSG_SimpleData<T>::exportData (char* buffer)
{
if(_convert)
#ifdef DSC_FORCE_MEMCPY_USE
// It is mandatory to use the memcpy function
{
static T val;
memcpy (&val, _dataPtr, _typeSize);
val = DSC_SwapFunc (val);
memcpy (buffer, &val, _typeSize);
}
#else
*(T*)buffer = DSC_SwapFunc (*_dataPtr);
#endif
else
#ifdef DSC_FORCE_MEMCPY_USE
// It is mandatory to use the memcpy function
memcpy (buffer, _dataPtr, _typeSize);
#else
*(T*)buffer = *_dataPtr;
#endif
return _nbOctets;
}
to repeat my problem quick: MSG_TemplateData is derived from MSG_Data and should therefore know all its variables like _convert... but somehow, it doesn't...
thank you for your time and answers!
alex
drkybelk
10-10-2006, 05:44 AM
Hi,
for starters: you got some raw pointers in your class MSG_Data, but neither do you have a copy constructor declared/defined nor an assignment operator. Your destructor does naught. So your class will cause you endless headache if you don't remedy that! The automatic copy/assignment only do a flat copy which means you destructor potentially deletes the same memory multiple times.
But for you original question: By looking at your code, you should be able to use [B]_convert as you suggest.
I haven't compiled your code, but your problem potentially arises from the fact that you include your *.cpp files into your header files. When dealing with template classes I'd suggest you only create header files and keep only the template(s) in there. Don't include a cpp in a header.
Hope that give you something to work on.
Cheers,
D
Danny
10-10-2006, 07:20 AM
OK, what I need is that you show us th exact line on which you get this error. One or two examples will do. The problem is that I can't associate line number with your code, since your code isn't complete. My pinpointed question is thgis: are you trying to use _convert and other members of the base class in a member initialization list of a derived class? In other words, are you trying to do something like this:
MSG_TemplateData(): _convert (false) {}
The problem basically stems from the mixing of ordinary classes and class templates in an inheritance chain. As a rule, templates and virtual functions don't go together. Additionally, templates and ordinary classes aren't a good mixture when it comes to inheritance. Either you use templates all the way (and in this case, inheritance is rather rare) or you stick to simle OO design i.e., inheriting from non-template classes. There are a few other issues in your code: you don't declare a virtual destructor in the base class, for instance, which is pretty dangerous.
grubera
10-10-2006, 11:23 AM
thank you for your inputs!
as I figured out, I still have a lot of work to do!!
thank you again and have a pleasant time!
bye alex
devx.com
Copyright Internet.com Inc. All Rights Reserved