LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2007, 08:51 AM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
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!
 
Old 08-07-2007, 11:49 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Last edited by wjevans_7d1@yahoo.co; 08-07-2007 at 11:50 AM.
 
Old 08-07-2007, 01:47 PM   #3
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
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.
 
Old 08-07-2007, 03:08 PM   #4
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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?
 
Old 08-09-2007, 12:50 AM   #5
1slipperyfish
LQ Newbie
 
Registered: May 2007
Location: wigan
Distribution: mandriva
Posts: 29

Rep: Reputation: 15
why don't you post it all then?
paul
 
Old 08-09-2007, 01:23 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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).
 
Old 08-09-2007, 08:49 AM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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).
 
Old 08-10-2007, 11:05 AM   #8
1slipperyfish
LQ Newbie
 
Registered: May 2007
Location: wigan
Distribution: mandriva
Posts: 29

Rep: Reputation: 15
i still can't figure out compile errors without some code or more code at least
paul
 
Old 08-10-2007, 03:48 PM   #9
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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?
 
Old 08-10-2007, 04:18 PM   #10
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Just so you know, BaseClass is an abstract class. I hope that doesn't make any difference?
 
Old 08-10-2007, 09:20 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?

Last edited by graemef; 08-10-2007 at 09:23 PM.
 
Old 08-11-2007, 03:21 AM   #12
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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.
 
Old 08-11-2007, 09:33 AM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 08-15-2007, 02:12 PM   #14
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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.
 
Old 08-15-2007, 02:28 PM   #15
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel compilation error: Error 15: File not found Niceman2005 Linux - General 9 10-04-2007 03:45 AM
Error during compilation!! vishamr2000 Linux - Distributions 3 03-21-2007 05:34 AM
compilation error Varadharajan Programming 9 02-04-2007 01:56 AM
Compilation Error abdullahgee Programming 9 05-21-2004 12:29 AM
Sqwebmail compilation error ... [maildirsearchC.o] Error 1 boogie_maan Linux - Software 0 10-26-2002 07:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration