LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Compilation Error (https://www.linuxquestions.org/questions/programming-9/c-compilation-error-575338/)

kamransoomro84 08-07-2007 08:51 AM

C++ Compilation Error
 
Hi,

I have a class BaseClass, and another one called DerivedClass. The problem is, I'm getting the following error while compiling:

Code:

error: expected class-name before ‘{’ token
where I use BaseClass. Putting in a forward declaration gives me:

Code:

error: forward declaration of ‘struct BaseClass’
error: invalid use of undefined type ‘struct BaseClass’

The declaration for DerivedClass is:

Code:

class DerivedClass : public BaseClass
I cannot make head nor tail of this error. Can someone please help me? Thanks!

wjevans_7d1@yahoo.co 08-07-2007 11:49 AM

Can you please post a complete, tiny program which illustrates the problem?

By "complete", I mean something that can be compiled exactly as you post it, without modifications.

By "tiny", I mean as small as possible and still be "complete" and still illustrate this problem, and only this one problem.

Please put the program in CODE markers:
  1. At the bottom of your editing window, click the Go Advanced button.
  2. In the new editing window, highlight your program source with your mouse.
  3. Click the # icon at the top of the new editing window.

mjones490 08-07-2007 01:47 PM

Yes, do post examples.

Off the top of my head, though, I'd say you're missing a semi-colon after the closing brace of your base class declaration.

kamransoomro84 08-07-2007 03:08 PM

Well, the problem is, I have no idea what's causing this problem, so I have no idea how to reproduce it :( Any suggestions on that?

1slipperyfish 08-09-2007 12:50 AM

why don't you post it all then?
paul

wjevans_7d1@yahoo.co 08-09-2007 01:23 AM

Posting it all is certainly one way forward.

A better way is to make a working temporary copy of the program, and strip as much out of that program as you can and still make the error occur.

You might find out at that point just what the error is without consulting us further. That would be cool.

If that doesn't happen, just post that small program (but complete, so we can compile it and play with it).

tuxdev 08-09-2007 08:49 AM

The problem is that BaseClass is only a forward declaration. Base classes must be completely declared types (or otherwise the calculations to figure out the size of the derived type wouldn't work, among other things).

1slipperyfish 08-10-2007 11:05 AM

i still can't figure out compile errors without some code:Pengy: or more code at least
paul

kamransoomro84 08-10-2007 03:48 PM

Very well then. Let me experiment how much of the code I can strip and still reproduce the error.

@tuxdev: I find your reasoning logical. However, I still do not understand how I can go about fixing this problem. Any suggestions?

kamransoomro84 08-10-2007 04:18 PM

Just so you know, BaseClass is an abstract class. I hope that doesn't make any difference?

graemef 08-10-2007 09:20 PM

The following simplified program works:

main.cpp
Code:

#include <iostream>
#include "DerivedClass.h"

using namespace std;

int main(int argc, char *argv[])
{
  BaseClass * bObj = new DerivedClass();
  cout << "bObj DerivedClass() : " << bObj->num() << endl;
  bObj = new BaseClass(5);
  cout << "bObj BaseClass(5)  : " << bObj->num() << endl;
  DerivedClass * dObj = new DerivedClass();
  cout << "dObj DerivedClass() : " << dObj->num() << endl;
  dObj->setNum(5);
  cout << "dObj after setNum(5): " << dObj->num() << endl;
  system("PAUSE");
  return 0;
}

BaseClass.h
Code:

#include <iostream>

using namespace std;

class BaseClass
{
  protected:
      int number;
  public:
      BaseClass(int num = 1){number = num;}
      int num () {return number;}
};

DerivedClass.h
Code:

using namespace std;

#include "BaseClass.h"

class DerivedClass : public BaseClass
{
  public:
      DerivedClass(int num = 1){}
      void setNum (int num = 1){number = num;}
};

However if you remove the #include from DerivedClass.h as follows:
Code:

using namespace std;

//#include "BaseClass.h"

class DerivedClass : public BaseClass
{
  public:
      DerivedClass(int num = 1){}
      void setNum (int num = 1){number = num;}
};

Then you get the following compile errors:

Code:

2 main.cpp In file included from main.cpp
6 DerivedClass.h expected class-name before '{' token
  DerivedClass.h In member function `void DerivedClass::setNum(int)':
9 DerivedClass.h `number' undeclared (first use this function)
 
... plus many more relating to main.cpp

So are you setting up and using header files correctly?

kamransoomro84 08-11-2007 03:21 AM

Yup, I've confirmed, and then reconfirmed that all the header files are being included and stuff. But I still can't figure out the problem. I'll get back to you guys with some code.

tuxdev 08-11-2007 09:33 AM

Then maybe your configure guard is messed up? The #define it keys off of has to be unique, and sometimes one forgets to change that if you copy-and-hack from another file.

kamransoomro84 08-15-2007 02:12 PM

I'm sorry? I didn't get that. Could you please elaborate? And I've stripped the code as much as I could. How do I give it to you guys? It's still too much to post here.

kamransoomro84 08-15-2007 02:28 PM

I'm sorry? I didn't get that. Could you please elaborate? And I've stripped the code as much as I could. How do I give it to you guys? It's still too much to post here.


All times are GMT -5. The time now is 01:06 PM.