Public Group

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

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

## Recommended Posts

##### 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 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 on other sites
Quote:
 Original post by quophyieYou 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 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 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!!!

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631705
• Total Posts
3001827
×