Inheritance problems

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>
#include "TwoDShape.h"
#include "Triangle.h"
#include "ColorTriangle.h"

using namespace std;

int main(){
	ColorTriangle t1("Blue", "right", 8.0,12.0);
	ColorTriangle t2("Red", "isosceles", 2.0, 2.0);

	cout << "Info for t1:\n";
	t1.showStyle();
	t1.showDim();
	t1.showColor();
	cout << "Area is " << t1.area() << "\n";

	cout << "\n";

	cout << "Info for t2: \n";
	t2.showStyle();
	t2.showDim();
	t2.showColor();
	cout << "Area is " << t2.area() << "\n";

	cin.get();
	return 0;
}


TwoDShape (.h and .cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
.h
class TwoDShape{
	double width;
	double height;
public:
	TwoDShape();
	TwoDShape(double w, double h);
	TwoDShape(double x);
	void showDim();
	double getWidth();
	double getHeight();
	void setWidth(double w);
	void setHeight(double h);
};
.cpp
#include <iostream>
#include "TwoDShape.h"

using namespace std;

TwoDShape::TwoDShape(){
	width=height=0.0;
}

TwoDShape::TwoDShape(double w, double h){
	width=w;
	height=h;
}

TwoDShape::TwoDShape(double x){
	width=height=x;
}

void TwoDShape::showDim(){
	cout << "Width and height are " << width << " and " << height << "\n";
}

double TwoDShape::getWidth() {return width;}
double TwoDShape::getHeight() {return height;}
void TwoDShape::setWidth(double w) {width=w;}
void TwoDShape::setHeight(double h) {height=h;}


Triangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
.h
#include "TwoDShape.h"

class Triangle : public TwoDShape{
	char style[20];
public:
	Triangle();
	Triangle(char *str, double w, double h) : TwoDShape(w,h);
	Triangle(double x) : TwoDShape(x);
	double area();
	void showStyle();
};
.cpp
#include <iostream>
#include <cstring>
#include "Triangle.h"

using namespace std;

Triangle::Triangle(){
	strcpy (style, "unknown");
}

Triangle::Triangle(char *str, double w, double h) : TwoDShape(w,h){
	strcpy(style, str);
}

Triangle::Triangle(double x) : TwoDShape(x){
	strcpy(style, "isosceles");
}

double Triangle::area(){
	return getWidth() * getHeight()/2;
}

void Triangle::showStyle(){
	cout << "Triangle is " << style << "\n";
}


ColorTriangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.h
#include "Triangle.h"

class ColorTriangle : public Triangle{
	char color[20];
public:
	ColorTriangle(char *clr, char *style, double w, double h) : Triangle(style, w, h);
	void showColor();
};
.cpp
#include <iostream>
#include <cstring>
#include "ColorTriangle.h"

using namespace std;

ColorTriangle::ColorTriangle(char *clr, char *style, double w, double h) : Triangle(style, w, h){
		strcpy(color, clr);
	}

void ColorTriangle::showColor(){
		cout << "Color is " << color << "\n";
	}
};


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.
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
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

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 };
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
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:
1
2
3
4
5
6
7
8
9
10
#ifndef __MyClass_Header__
#define __MyClass_Header__

#include "ThatClass.h"

class MyClass : public ThatClass
{
};

#endif 


Could you also post your updated code since you made some changes?
Last edited on
Thanks LB. I didn't think include guards would make such a difference but they seem to have.


- 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.

This is what I am still looking at.

Code now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef	COLORTRIANGLE_H
#define COLORTRIANGLE_H

#include "Triangle.h"

class ColorTriangle : public Triangle{
	char color[20];
public:
	ColorTriangle(char *clr, char *style, double w, double h) : Triangle(style, w, h){};
	void showColor();
};

#endif

#include <iostream>
#include <cstring>
#include "ColorTriangle.h"

using namespace std;

ColorTriangle::ColorTriangle(char *clr, char *style, double w, double h){
	strcpy(color, clr);
}

void ColorTriangle::showColor(){
	cout << "Color is " << color << "\n";
}

#ifndef TRIANGLE_H
#define TRIANGLE_H

#include "TwoDShape.h"

class Triangle : public TwoDShape{
	char style[20];
public:
	Triangle();
	Triangle(char *str, double w, double h) : TwoDShape(w,h){};
	Triangle(double x) : TwoDShape(x){};
	double area();
	void showStyle();
};

#endif

#include <iostream>
#include <cstring>
#include "Triangle.h"

using namespace std;

Triangle::Triangle(){
	strcpy (style, "unknown");
}

Triangle::Triangle(char *str, double w, double h) : TwoDShape(w,h){
	strcpy(style, str);
}

Triangle::Triangle(double x) : TwoDShape(x){
	strcpy(style, "isosceles");
}

double Triangle::area(){
	return getWidth() * getHeight()/2;
}

void Triangle::showStyle(){
	cout << "Triangle is " << style << "\n";
}

#ifndef TWODSHAPE_H
#define TWODSHAPE_H

class TwoDShape{
	double width;
	double height;
public:
	TwoDShape();
	TwoDShape(double w, double h);
	TwoDShape(double x);
	void showDim();
	double getWidth();
	double getHeight();
	void setWidth(double w);
	void setHeight(double h);
};

#endif

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

using namespace std;

TwoDShape::TwoDShape(){
	width=height=0.0;
}

TwoDShape::TwoDShape(double w, double h){
	width=w;
	height=h;
}

TwoDShape::TwoDShape(double x){
	width=height=x;
}

void TwoDShape::showDim(){
	cout << "Width and height are " << width << " and " << height << "\n";
}

double TwoDShape::getWidth() {return width;}
double TwoDShape::getHeight() {return height;}
void TwoDShape::setWidth(double w) {width=w;}
void TwoDShape::setHeight(double h) {height=h;}


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
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.
I don't think you understood LB's post. See my comments.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	Triangle(char *str, double w, double h) : TwoDShape(w,h){};//this is a definition becuase of the curly braces
	Triangle(double x) : TwoDShape(x){};//so is this


//they are re-defined here, hence the "function has a body errors.
//remove the braces from the headers for functions you define in the cpp file.
Triangle::Triangle(){
	strcpy (style, "unknown");
}

Triangle::Triangle(char *str, double w, double h) : TwoDShape(w,h){
	strcpy(style, str);
}

Triangle::Triangle(double x) : TwoDShape(x){
	strcpy(style, "isosceles");
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
class MyClass
{
    int v;
public:
    MyClass(int x) : v(x) {} //this is an inline definition
    MyClass(int x, int y); //this is a declaration, no colon : part
};

MyClass::MyClass(int x, int y) : v(x+y) //note that the colon part is HERE not in the class
{
}
Topic archived. No new replies allowed.