| musico (9) | |||||||||
|
I am trying to figure out inheritance using separate files for headers. This is a program from an old book that was written all in one file, I am trying to rewrite it for separate files. It is the inheritance I am stumbling on, the rest is very clear, main.cpp
TwoDShape (.h and .cpp)
Triangle
ColorTriangle
It is in VC++ 2012 Express. Main errors are about base class undefined. Any ideas? I can do programs like this without inheritance so obviously that is where I'm going wrong. | |||||||||
|
|
|||||||||
| L B (3325) | |
| Copy and paste the exact errors you are getting. Also, for the love of C++ use std::string instead of character arrays and character pointers... | |
|
Last edited on
|
|
| musico (9) | |
|
Part 1 Error 1 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 7 1 Inheritance Error 2 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 3 error C2059: syntax error : 'inline function header' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 4 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 5 error C2059: syntax error : 'using' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 5 1 Inheritance Error 6 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 5 1 Inheritance Error 7 error C2039: '{ctor}' : is not a member of 'Triangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 7 1 Inheritance Error 8 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 7 1 Inheritance Error 9 error C2614: 'Triangle' : illegal member initialization: 'Triangle' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 7 1 Inheritance Error 10 error C2084: function 'Triangle::Triangle(char *,double,double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 11 1 Inheritance Error 11 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 24 1 Inheritance Error 12 error C2011: 'TwoDShape' : 'class' type redefinition c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 1 1 Inheritance Error 13 error C2504: 'TwoDShape' : base class undefined c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 3 1 Inheritance Error 14 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 7 1 Inheritance Error 15 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 16 error C2059: syntax error : 'inline function header' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 17 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 18 error C2059: syntax error : '<class-head>' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 1 1 Inheritance Error 19 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 1 1 Inheritance Error 20 error C2614: 'Triangle' : illegal member initialization: 'TwoDShape' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 1 1 Inheritance Error 21 error C2143: syntax error : missing ';' before 'public' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 4 1 Inheritance Error 22 error C2144: syntax error : 'double' should be preceded by ')' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 6 1 Inheritance Error 23 error C2059: syntax error : ')' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 6 1 Inheritance Error 24 error C2144: syntax error : 'double' should be preceded by ')' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 7 1 Inheritance Error 25 error C2059: syntax error : ')' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 7 1 Inheritance Error 26 error C2011: 'Triangle' : 'class' type redefinition c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 3 1 Inheritance Error 27 error C2504: 'Triangle' : base class undefined c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 3 1 Inheritance Error 28 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 6 1 Inheritance Error 29 error C2059: syntax error : 'using' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 7 1 Inheritance Error 30 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 7 1 Inheritance Error 31 error C2062: type 'int' unexpected c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 9 1 Inheritance Error 32 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 9 1 Inheritance Error 33 error C2614: 'ColorTriangle' : illegal member initialization: 'Triangle' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 9 1 Inheritance Error 34 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 13 1 Inheritance Error 35 error C2039: 'showStyle' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 14 1 Inheritance Error 36 error C2039: 'showDim' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 15 1 Inheritance Error 37 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 17 1 Inheritance Error 38 error C2039: 'area' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 17 1 Inheritance Error 39 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 19 1 Inheritance Error 40 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 21 1 Inheritance Error 41 error C2039: 'showStyle' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 22 1 Inheritance | |
|
|
|
| musico (9) | |
|
Part 2 Error 42 error C2039: 'showDim' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 23 1 Inheritance Error 43 error C2065: 'cout' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 25 1 Inheritance Error 44 error C2039: 'area' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 25 1 Inheritance Error 45 error C2065: 'cin' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 27 1 Inheritance Error 46 error C2228: left of '.get' must have class/struct/union c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 27 1 Inheritance Error 47 error C2534: 'ColorTriangle' : constructor cannot return a value c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 28 1 Inheritance Error 48 error C2562: 'ColorTriangle::ColorTriangle' : 'void' function returning a value c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 28 1 Inheritance Error 49 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 7 1 Inheritance Error 50 error C2969: syntax error : ';' : expected member function definition to end with '}' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 51 error C2059: syntax error : 'inline function header' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 52 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 53 error C2059: syntax error : '<class-head>' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 3 1 Inheritance Error 54 error C2630: ';' found in what should be a comma-separated list c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 3 1 Inheritance Error 55 error C2143: syntax error : missing ';' before 'public' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 5 1 Inheritance Error 56 error C2653: 'ColorTriangle' : is not a class or namespace name c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 7 1 Inheritance Error 57 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 7 1 Inheritance Error 58 error C2550: 'ColorTriangle' : constructor initializer lists are only allowed on constructor definitions c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 7 1 Inheritance Error 59 error C2065: 'color' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 8 1 Inheritance Error 61 error C2653: 'ColorTriangle' : is not a class or namespace name c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 11 1 Inheritance Error 62 error C2065: 'color' : undeclared identifier c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 12 1 Inheritance And I got rid of the final }; | |
|
|
|
| musico (9) | |
|
I changed the ; to {} in the constructor definitions of the subclass headers and that cleared up a lot. Now I have. Error 2 error C2084: function 'Triangle::Triangle(char *,double,double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 11 1 Inheritance Error 3 error C2084: function 'Triangle::Triangle(double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 15 1 Inheritance Error 4 error C2011: 'TwoDShape' : 'class' type redefinition c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\twodshape.h 1 1 Inheritance Error 5 error C2504: 'TwoDShape' : base class undefined c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 3 1 Inheritance Error 6 error C2614: 'Triangle' : illegal member initialization: 'TwoDShape' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 7 1 Inheritance Error 7 error C2614: 'Triangle' : illegal member initialization: 'TwoDShape' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 8 1 Inheritance Error 8 error C2011: 'Triangle' : 'class' type redefinition c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.h 3 1 Inheritance Error 9 error C2504: 'Triangle' : base class undefined c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 3 1 Inheritance Error 10 error C2614: 'ColorTriangle' : illegal member initialization: 'Triangle' is not a base or member c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.h 6 1 Inheritance Error 11 error C2039: 'showStyle' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 14 1 Inheritance Error 12 error C2039: 'showDim' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 15 1 Inheritance Error 13 error C2039: 'area' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 17 1 Inheritance Error 14 error C2039: 'showStyle' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 22 1 Inheritance Error 15 error C2039: 'showDim' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 23 1 Inheritance Error 16 error C2039: 'area' : is not a member of 'ColorTriangle' c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\main.cpp 25 1 Inheritance Error 17 error C2084: function 'ColorTriangle::ColorTriangle(char *,char *,double,double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 7 1 Inheritance | |
|
|
|
| L B (3325) | |||
|
OK, a few notes: - Don't use the colon operator on constructors without the curly brace definitions. The part after the colon is considered to technically be part of the definition, so you can's split that from the rest. - Make sure you only define the member functions once - either make them all inline cass foo { void bar(){} }; or all separate void foo::bar(){}, not both.- Make sure you use include guards:
Could you also post your updated code since you made some changes? | |||
|
Last edited on
|
|||
| musico (9) | ||||
Thanks LB. I didn't think include guards would make such a difference but they seem to have.
This is what I am still looking at. Code now
main.cpp as before. Errors Error 2 error C2084: function 'Triangle::Triangle(char *,double,double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 11 1 Inheritance Error 3 error C2084: function 'Triangle::Triangle(double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\triangle.cpp 15 1 Inheritance Error 4 error C2084: function 'ColorTriangle::ColorTriangle(char *,char *,double,double)' already has a body c:\users\novatekk\documents\visual studio 2012\projects\inheritance\inheritance\colortriangle.cpp 7 1 Inheritance | ||||
|
|
||||
| musico (9) | |
|
I might be missing something obvious but I can't get round this problem of 'already has a body'. Removing the offending lines from the .cpp files allows it to compile and run but of course the data is not passed into ColorTriangle that way. I have replaced the strcpy lines with string which looks tidier, not that that solves the main problems here. | |
|
|
|
| naraku9333 (919) | |||
I don't think you understood LB's post. See my comments.
| |||
|
Last edited on
|
|||
| L B (3325) | |||
| |||
|
|
|||