Problem with includes

I have a simple program, but I can't get the classes to recognize each other. I have a Point class:

Point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
using namespace std;

class Point{
private:
	int x;
	int y;

public:
	Point();

	Point(int, int);

	~Point();

	int getX();
	int getY();
	void setX(int);
	void setY(int);
};


with an accompanying Point.cpp implementation.

Then I have a Rectangle class:

Rectangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>

using namespace std;

class Rectangle{

private:
	Point lowerLeftCorner;
	Point upperRightCorner;

public:
	Rectangle();

	Rectangle(Point, Point);

	~Rectangle();

	int getWidth();
	int getHeight();
	int getArea();

	bool isSquare();
};


with an implementation:

Rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Rectangle.h"
#include "Point.h"

Rectangle::Rectangle(){
	lowerLeftCorner = Point(0,0);
	upperRightCorner = Point(10,10);
}

Rectangle::Rectangle(Point lowerLeftCornerInput, Point upperRightCornerInput) {
	lowerLeftCorner = lowerLeftCornerInput;
	upperRightCorner = upperRightCornerInput;
}

...


My problem is that my IDE tells me that line 9 of Rectangle.cpp is wrong because "Error: member "Rectangle::Point" is not a type name". What do I need to do to get Rectangle to recognize Point?

Thanks,

Dane
Below is what your code looks like to the compiler. Do you see that this Point lowerLeftCorner; is reached before the compiler has ever heard of the object Point? That is why it complains. You need to inform it of Point before you try to use one. I suggest you put #include "Point.h" at the top of "Rectangle.h". I also suggest you start using header guards: http://en.wikipedia.org/wiki/Include_guard

As an aside, please do no ever put using namespace std; in your header files. It is considered bad form, for good reasons.


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
#include <string>

using namespace std;

class Rectangle{

private:
	Point lowerLeftCorner;
	Point upperRightCorner;

public:
	Rectangle();

	Rectangle(Point, Point);

	~Rectangle();

	int getWidth();
	int getHeight();
	int getArea();

	bool isSquare();
};

#include <string>
using namespace std;

class Point{
private:
	int x;
	int y;

public:
	Point();

	Point(int, int);

	~Point();

	int getX();
	int getY();
	void setX(int);
	void setY(int);
};

Rectangle::Rectangle(){
	lowerLeftCorner = Point(0,0);
	upperRightCorner = Point(10,10);
}

Rectangle::Rectangle(Point lowerLeftCornerInput, Point upperRightCornerInput) {
	lowerLeftCorner = lowerLeftCornerInput;
	upperRightCorner = upperRightCornerInput;
}

...
Last edited on
Thanks Moschops. I've added the #include "Point.h" directive to Rectangle.h, but now I get this error:

1
2
3
4
1>c:\users\dane\documents\visual studio 2010\projects\main\main\point.h(4): error C2011: 'Point' : 'class' type redefinition
1>          c:\users\dane\documents\visual studio 2010\projects\main\main\point.h(4) : see declaration of 'Point'
1>c:\users\dane\documents\visual studio 2010\projects\main\main\rectangle.h(11): error C2079: 'Rectangle::lowerLeftCorner' uses undefined class 'Point'
1>c:\users\dane\documents\visual studio 2010\projects\main\main\rectangle.h(12): error C2079: 'Rectangle::upperRightCorner' uses undefined class 'Point'


I'm guessing that I've somehow defined Point twice. What determines the order that the classes compile? In your code block above, you put that Rectangle attempts to compile before Point. Why is that?

Thanks again,

Dane
In your code block above, you put that Rectangle attempts to compile before Point.


When compilation starts, each cpp file is taken in turn. The pre-processor scans through it, and everywhere it finds a #include<someFile> it finds someFile and copies it completely, into the cpp file, at that line. Your cpp file included Rectangle.h and then Point.h, so when those files are copied into the cpp file, Rectangle.h is copied first, and thus is before Point.h

Did you remember to use guards? http://en.wikipedia.org/wiki/Include_guard
They will stop you repeating header files through the code. If Rectangle.h includes Point.h (which, as I explained above, basically means a complete copy of Point.h exists at the start of Rectangle.h), and then Rectangle.cpp also includes Point.h, you end up with Point.h copied out twice, which means your definition of Point.h exists twice - hence the redefinition error. The guards can stop this happening. http://en.wikipedia.org/wiki/Include_guard

Once you understand that #include<someFile> creates an exact, complete copy of <someFile> at that point, it all becomes much clearer.


Thanks, that was the trick!
Topic archived. No new replies allowed.