Inheritance problem

Hi all!

I really thought that I understood the aspects of inheritance, but when I run this program: http://pastebin.com/P51bzYdf , so there must be that I did not understand it after all :(. Could anyone please explain?

I end up with some strange linker errors saying:

1
2
3
4
5
6
7
8
9
10
11
1
1>Compiling...
1>mainfunc.cpp
1>Linking...
1>mainfunc.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Shape::toString(void)" (?toString@Shape@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>mainfunc.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Shape::getColor(void)" (?getColor@Shape@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>mainfunc.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Shape::getLine(void)" (?getLine@Shape@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>mainfunc.obj : error LNK2019: unresolved external symbol "public: __thiscall Shape::Shape(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Shape@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "public: __thiscall Circle::Circle(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double)" (??0Circle@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0N@Z)
1>"path" : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at "..."
1>
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks a bunch!

I might as well provide the sourcecode here:

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
///////////////////
//shape.h:
///////////////////

#ifndef SHAPE_H
#define SHAPE_H

#include <string>
#include <cmath>
#include "part2.h"

using namespace std;

#define PI 4.0 * atan(1.0) 	//This line enables you to use PI in your calculations

class Shape {
private:
	string color;
	string line;
public:
	//Shape(){}
	Shape(string color, string line);	//Constructor
	void setColor(string color);		//Used to change the color
	void setLine(string line);			//Used to change the line type
	string getColor();				//Returns the color
	string getLine();				//Returns the line type
	string toString();				//Returns a string that descibes the shape in this format "Color = blue, Line = dotted"
}; 

#endif

/*---------------------------*/


///////////////////
//shape.cpp:
///////////////////
#include <string>
#include <sstream>

#include "shape.h"

using namespace std;


Shape::Shape(string color, string line){
	this->color = color;
	this->line = line;
}

void Shape::setColor(string color){
	this->color = color;
}

string Shape::getColor(){
	return color;
}

void Shape::setLine(string line){
	this->line = line;
}

string Shape::getLine(){
	return line;
}

string Shape::toString(){
	stringstream ss;
	ss << "Color = " << color << ", Line = " << line;
	return ss.str();
}

/*---------------------------*/


///////////////////
//part2.h:
///////////////////

#include "shape.h"

using namespace std;

class Circle : public Shape
{
private:
	double radius;
public:
	//Circle() : Shape(){ }
	Circle(string color, string line, double radius) : Shape(color, line)	, radius(radius) { }
	void setRadius(double radius)
	{
		if(radius<0) 
			this->radius=0; 
		else 
			this->radius=radius;
	}
	double getRadius(){return radius;}
	double getCirc(){return 2*PI*radius;}
	double getArea(){return radius*radius*PI;}
};

/*---------------------------*/


///////////////////
//mainfunc.cpp:
///////////////////

#include "part1.h"
#include "shape.h"
#include "part2.h"
#include <iostream>
#include <string>

using namespace std;
	
int main()
{
	// Inheritance problem

	//Circle rund("red", "dotted", 2);
	Circle* rund = new Circle("red", "dotted", 2);
	
	cout << "\nFeatures of the circle using the Circle object's get-funcs:" << endl;
	cout << "Color: " << rund->getColor() << ", Line: " << rund->getLine();
	cout << ", Radius: " << rund->getRadius() << endl;
	cout << "Features of the circle using the Shape class' toString-func:" << endl;
	cout << rund->toString() << endl;

	delete rund;

	return 0;
}
You probably haven't put all the files in the project. (You missed shape.cpp). But you can actually do it in one file. Just copy-paste this here in your compiler and remove any line writing:

#include "part1.h" or
#include "part2.h" or
#include "shape.h"

It worked for me.
Thanks!
It turns out that the code was right, but I had messed up the #include's. It worked when I sat up the #indef-#endif-thingy. Has anyone a general rule to follow when one is including stuff?
If you need to use stuff inside of it, you will need to include it. If you just need a pointer or reference (for example when writing a class), then you just need to forward declare it.

Look here for more info: http://www.cplusplus.com/forum/articles/10627/
Topic archived. No new replies allowed.