Advertisement

c++ - base class undefined problem...

Started by August 09, 2000 03:44 AM
4 comments, last by Poontardis 24 years, 4 months ago
Ok, does anyone want to point out where I'm going wrong here? The code below is a simplified version of part of my application. Unfortunately, I get a "base class undefined" error when I try to compile.
        
// MyThing.h /////////////////////////////////


#ifndef _MYTHING_H_
#define _MYTHING_H_


#include <windows.h>

#include .... blah....


class cMyThing;


class cMyThing
{
public:
    cMyThing() {};
    cMyThing(int);
    int myval;
protected:
    int myval2;
};

#endif


//////////////////////////////////////////////



// MyThing.cpp ///////////////////////////////



#include "MyThing.h"


cMyThing::cMyThing(int i)
{
    myval = i;
    myval2 = i << 1;
}

//////////////////////////////////////////////



// MyOtherThing.h ////////////////////////////



#ifndef _MYOTHERTHING_H_
#define _MYOTHERTHING_H_


#include <windows.h>

#include "MyThing.h"

#include .... blah....


class cMyOtherThing;


class cMyOtherThing : public cMyThing  //This line gives the compiler error "base class undefined"

{
public:
    cMyOtherThing(int);
protected:
    int myval3;
};

#endif

//////////////////////////////////////////////



// MyOtherThing.cpp //////////////////////////



#include "MyOtherThing.h"


cMyOtherThing::cMyOtherThing(int i)
{
    myval3 = i << 2;
}

//////////////////////////////////////////////        
Any ideas, I can't think what I'm missing out Help! Edited by - poontardis on 8/9/00 3:47:27 AM
The:
class cMyOtherThing;

and

class cMyThing;

should not be nesessary.I dont see any errors in the code.
Ries
Advertisement
Yeah, I don''t know why I put them in really.

The problem is, that the example I gave above would probably compile. My real code on the other hand is far to big to post here (few thousand lines) and won''t compile.

I think it''s something to do with the order in which VC++ looks at header files. Even though I have #included the appropriate header file, it doesn''t seem to want to let me use the classes and types defined in that file.

for example:


...
#include "CGDATerrain.h"
class cCROAMTerrain: public cTerrain
{
//blah...
}

does not compile nor does:

#include "CGDATerrain.h"
class cCROAMTerrain
{
SurroundingHeightsRec Heights;
}
...

where cTerrain and SurroundingHeightsRec are both declared and defined in CGDATerrain.h|cpp.

Both of the above cases cause a "base class undefined" or an "undefined idenitifier" error.

The lines above are from ROAM.h.
ROAM.h is #included in quite a lot of other files in the project, but CGDATerrain.h is only #included in ROAM.h.
The compiler doesn''t seem to include the data from CGDATerrain.h, but I can''t see why it would do that.

Oh I''m confused.
It might be reaching a bit, but make sure that the #ifdef DEFVAR isn''t used/defined somewhere else. Are all the .h files local to the directory where they are called?
It''s not that the DEFVAR is already used.

I''ve got my files in two directories:
The project dir, and a common dir which has the source for useful functions + so on.

This isn''t a major problem anymore, as I''ve decided that inheritance was a stupid idea as I''d end up having to rewrite most of the functions and would have massive overheads I didn''t need, so I''ve changed my approach to creating a brand new class with bits of code cut and pasted from the other class.

Still, I''d still like to know what the hell was going on so that I can avoid it in future.
Most compilers have an option to spit out the pre-processed version of your file (with VC it''s /P). The pre-processed version is one huge file with all of your #include''s embedded in it, macros expanded, etc. Then you can look through the file to find where you think you''re base class should have been defined and maybe get a clue as to what''s up.

VC will put #line statements in the pre-processed file that tells you exactly what file and line stuff came from. If you see that your base class definition is skipped, then look at these #line''s to see find where the compiler decided to start skipping stuff.

Another trick is to use #error or the equivalent. It seems likely that perhaps you defined a macro where you shouldn''t have or something. So what you can do is:

#ifdef FOO
#error FOO shouldn''t be defined yet!
#endif

Then you''ll get a compiler error if things aren''t right. Sort of the pre-processor version of debugging by sprinkling printf''s in your code.

-Mike

This topic is closed to new replies.

Advertisement