![]() |
compiling error
Hi,
I have a compiling error but I do not understand why: I have 2 class Rectangle and Screen. I have to code a function in class rectangle whose the prototype is: void Rectangle::draw( Screen object) Since the class Screen is the argument of the function, I set friend class Screen in the public section of the class Rectangle. :
void Rectangle::draw( Screen object)1-`object' has incomplete type 2-forward declaration of `struct Screen' // refer to line where I declared friend class Screen 3-invalid use of undefined type `struct Screen' Please, can someone please tell me the problem? B. |
Its basically saying that it doesn't know what a Screen object is, which means you haven't declared it yet. Try moving your definition of Screen to above the definition of the Rectangle:draw function (or maybe you need to include the header file - I can't tell from this snippet).
|
Quote:
:
void Rectangle::draw( Screen object):
`char Screen::pixels[4][5]' is privateI don't know why I have this error. it is like It cannot access it. I tried to make friend class Rectangle in class Screen the I get a linker error. Dont't know what to do here! B. |
We appreciate your trying to reduce your problem area to a snippet, but I believe you should show your definitions. There are a number of questionable areas.
|
Quote:
Also note that :
object.pixels[x1][y1]; |
Quote:
:
Perhaps I have not coded properly what Rectangle::draw(void) is supposed to do. Thank you |
If you want class Rectangle to be a friend class to Screen, put
:
friend class Rectangle;You will need to tell the compiler that Rectangle is a class before you try use it as a friend. This is called forward declaring. Put :
class Rectangle;A better idea might be that the Rectangle class should be using the Screen class setPoint member function instead of jumping into the internals of the Screen class. Also note that this line in setPoint :
pixels[i][j]='c'; |
Quote:
but when I put friend class rectangle in Screen, I get a linker error. [Linker error] undefined reference to `Rectangle::~Rectangle()' [Linker error] undefined reference to `Point::~Point()' and the same error that before is not gone. |
Well you have declared ~Rectangle, but you haven't defined it.
You need to add :
Rectangle::~Rectangle()Also, there is no need to quote my entire post - it just make the web page bigger. |
I have no longer have the linker error, still the error is present!
Good god it does not go away! Do you think that I did make a mistake in the defining the function? |
| All times are GMT -5. The time now is 12:48 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC