Array of Pointers to Object in a Class

** This is a repost, I posted in Beginners section but got no response, yet...

Hi All,

I'm trying to make an array of pointers that point to an object class that I've created in another class. More specifically, I have a class "SkipList" in which I want an array of pointers to an object of class "Rectangle".

Here's a snippet of code that I have:

rectangle.h
1
2
3
4
5
6
7
8
9
10
11
class Rectangle {
	public:
		Rectangle(char* name, int x, int y, int w, int h);    
		~Rectangle();
		char *name;
		int x;
		int y;
		int w;
		int h;
		Rectangle *forward[10];
};


skiplist.h
1
2
3
4
5
6
7
8
9
10
11
12
class SkipList {
	public:
		SkipList();
        	~SkipList();
		void insert(char* name, int x, int y, int w, int h);    
		void remove(char* name);
		void remove(int x, int y, int w, int h);
		void rangesearch(int x, int y, int w, int h);
		void allintersections();
		void search(char* name);
		Rectangle *header[10];
};


When I try to compile it, it didn't like the array of pointers in "skiplist.h" and gave me the following errors:

skiplist.h(11) : error C2143: syntax error : missing ';' before '*'
skiplist.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Yes, this is an assignment that I am working on, any help/hints would be greatly appreciated. Thanks!
Last edited on
Solved. I had to add

 
#include"rectangle.h" 


in skiplist.h
I'm only a C++ novice, but I would say that since you are not declaring or using any actual Rectangle objects in the definition of SkipList, you should just use a forward declaration of Rectangle...

1
2
3
4
5
6
7
class Rectangle;

class SkipList
{
  ...
  Rectangle* header[10];
};


If you use anything from Rectangle in your implementation of SkipList (conventionally in skiplist.cpp), then include rectangle.h just before the implementation.

For example...

 
float Area = header[2]->GetArea();


...would require some details about the Rectangle class and not just the fact that the Rectangle class exists.

Dave
Topic archived. No new replies allowed.