Sign in to follow this  

Help with Inheritance "error: expected class-name before '' token"

This topic is 3667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys, I am new to this forum and site and hence I may be doing a few things wrong so pardon me if you find i'm doing stuff wrong. Anyway I am new to C plus plus but I have have a very good Java background so I understand most of the concepts. My problem is to do with inheritance. I have two classes in a project called SanthiProject i.e. the base class is called vehicle and the derived class is called boat which are both saved in different cpp files. The thing is when I compile I get the error message below "..\boat.h:9: error: expected class-name before '{' token". The funny thing is I have two other classes in a different project(Helloworld Project) where one derives from the other but that works fine. The only difference between the two projects is that in the base class in the SanthiProject has a virtual function called " virtual message(void)" which the derived class overides. I dont think that this should make a difference but it seems to. Also when I add the boat class code to the vehicle.cpp file and have the boat class derive from the vehicle class in the same cpp file, it works with no problems at all. Please can Some one help me cos I have no idea what I am doing wrong and I've trawled the web but cant seem to find any anwers. I am using eclipse 3.3 and the tool chain is MinGW 3.4.5. The compiler output is below **** Build of configuration Debug for project SanthiProject **** **** Internal Builder is used for build **** g++ -O2 -g -Wall -c -fmessage-length=0 -oboat.o ..\boat.cpp In file included from D:/C Files 02.03.2007/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/iostream.h:31, from ..\boat.cpp:5: D:/C Files 02.03.2007/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. ..\boat.cpp:37:7: warning: no newline at end of file g++ -O2 -g -Wall -c -fmessage-length=0 -ovehicle.o ..\vehicle.cpp In file included from ..\vehicle.cpp:5: ..\boat.h:9: error: expected class-name before '{' token ..\vehicle.cpp:50:4: warning: "/*" within comment ..\vehicle.cpp:80:22: warning: no newline at end of file Build error occurred, build is stopped Time consumed: 4093 ms. The vehicle class i.e. the base class cpp and header files are below vehicle cpp file #ifndef VEHICLE_H_ #define VEHICLE_H_ #include <iostream> #include "vehicle.h" #include "boat.h" using namespace std; //class boat; class vehicle { int wheels; float weight; public: vehicle(); virtual ~vehicle(); virtual void message(void) {cout << "This is a Vehicle" endl;} }; vehicle::vehicle(){} vehicle::~vehicle(){} #endif /*VEHICLE_H_*/ The Vehicle header file #ifndef VEHICLE_H_ #define VEHICLE_H_ //#include "boat.h" class boat; class vehicle { public: vehicle(); virtual ~vehicle(); virtual message(void); }; #endif /*VEHICLE_H_*/ The boat class(the derived class) cpp file #ifndef BOAT_H_ #define BOAT_H_ #include "boat.h" #include "vehicle.h" #include <iostream.h> using namespace std; class boat { int passenger_load; public: boat(); virtual ~boat(); int passengers(void) {return passenger_load;} void message(void) {cout << "Boat message\n";} }; boat::boat() { } boat::~boat() { } int main() //vehicle *unicycle; { boat *bt = new boat; bt->message(); delete bt; // delete unicycle; return 0; } #endif The boat header file: #ifndef BOAT_H_ #define BOAT_H_ #include <iostream> #include "vehicle.h" class boat:public vehicle { public: boat(); virtual ~boat(); void message(void); }; #endif /*BOAT_H_*/

Share this post


Link to post
Share on other sites
First, you shouldn't be using theis sort of stuff in the .cpp file:

#ifndef VEHICLE_H_
#define VEHICLE_H_




That should only go in the header, otherwise the compiler won't be including the header at all. It'll load the header file, come across the above statement, see that VEHICLE_H_ is already #defined, and ignore everything down to the #endif.

Secondly, you don't tell the compiler that a boat is a vehicle. Instead of:
class boat
It should be:
class boat : public vehicle

Thirdly, you should use [ source ] tags when posting chunks of code, since it makes it easier to read.

Share this post


Link to post
Share on other sites
You beauty!! Evil Steve!!!, Thank you so much. Worked just as you suggested. You have no idea how much this is appreciated. I've been going around in circles and banging my head for days!!. With regards to

#ifndef VEHICLE_H_
#define VEHICLE_H_

in .cpp files. it seems that if I dont include that in the file, I get an error saying "redefinition of class". It only seems to go away if I include it in both the .cpp file and the header file. Does anyone know why this is the case?the compiler output is as follws. Thanks again for the inheritance stuff

**** Build of configuration Debug for project SanthiProject ****

**** Internal Builder is used for build ****
g++ -O2 -g -Wall -c -fmessage-length=0 -oboat.o ..\boat.cpp
In file included from D:/C Files 02.03.2007/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/iostream.h:31,
from ..\boat.cpp:5:
D:/C Files 02.03.2007/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
..\boat.cpp:10: error: redefinition of `class boat'
..\boat.h:9: error: previous definition of `class boat'
..\boat.cpp:19: confused by earlier errors, bailing out
Build error occurred, build is stopped
Time consumed: 8625 ms.

Share this post


Link to post
Share on other sites
Quote:
Original post by quophyie
You beauty!! Evil Steve!!!, Thank you so much. Worked just as you suggested. You have no idea how much this is appreciated. I've been going around in circles and banging my head for days!!. With regards to

#ifndef VEHICLE_H_
#define VEHICLE_H_

in .cpp files. it seems that if I dont include that in the file, I get an error saying "redefinition of class". It only seems to go away if I include it in both the .cpp file and the header file. Does anyone know why this is the case?the compiler output is as follws. Thanks again for the inheritance stuff


That would be because you are defining the boat class 3 times and the vehicle class twice.

Share this post


Link to post
Share on other sites
It looks like you are putting the class definition in the .cpp and .h. The class interface should only be in the .h


class vehicle
{
int wheels;
float weight;
public:
vehicle();
virtual ~vehicle();
virtual void message(void) {cout << "This is a Vehicle" endl;}

};



That stuff should not be in the .cpp, it should only be in the .h. This goes for the boat header as well.

Hope this helps

Share this post


Link to post
Share on other sites
Thanks jeff75225 and ExcessNeo. It looks like the problem was also including

#ifndef VEHICLE_H_
#define VEHICLE_H_

in the .cpp file file as well as the header file. When I removed it it worked like a dream. I should have realised it cos this should ideally only be included in the header file. School boy errors!!!. Thank you guys so much!!!. very much appreciated!!!

Share this post


Link to post
Share on other sites

This topic is 3667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this