![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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)
{
int x1=upLeft.getX();
int y1=upLeft.getY();
for(int i=x1;i<(x1+width);i++)
for(int j=y1;j<(y1+height);j++)
object.pixels[x1][y1];//error 1 and 31-`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. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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).
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Quote:
void Rectangle::draw( Screen object)
{
int x1=upLeft.getX();
int y1=upLeft.getY();
for(int i=x1;i<(x1+width);i++)
for(int j=y1;j<(y1+height);j++)
object.pixels[x1][y1];
// pixels must be the private data member of the class Screen.
it is a char pixels[4][5];
}`char Screen::pixels[4][5]' is private I 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. |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
Quote:
Also note that object.pixels[x1][y1]; |
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Quote:
class Point{// Implementation not shown
private:
int x;
int y;
public:
Point();
Point(int,int);
~Point();
void setPoint(int,int);
int getX(void) const;
int getY(void) const;
};
// class screen
class Screen
{
private:
char pixels[20][30];
public:
//friend class Rectangle; produce linker error
Screen(void);
~Screen(void);
void setPoint(const Point&,char);
void resetPoint(Point);
void clear(void);
void draw(void);// print contents of the screen on monitor
};
// implementation of screen class
Screen::Screen()
{
int i,j;
for(i=0;i<20;i++){
for(j=0;j<30;j++){
pixels[i][j]='.';}}
}
Screen::~Screen(){}
void Screen::setPoint(const Point & object, char c)
{
int i=object.getX();
int j=object.getY();
pixels[i][j]='c';
}
void Screen::draw()
{
for( int i=0;i<20;i++)
{for( int j=0;j<30;j++)
{cout<<pixels[i][j];}
cout<<endl;
}
}
class Rectangle{
private:
int height;
int width;
Point upLeft;// upper left corner of a rectangle
char Paint;// char to paint the rectangle
public:
friend class Screen;
Rectangle(void);
Rectangle(const Point&,int,int,char);
~Rectangle();
void set(const Point&,int,int,char);//set values of existing rectangle
void setPaint(char);//set the character to paint the rectangle
void setPoint(const Point&);//sets upper left corner of rectangle
void setPoint(int, int);//sets upper left corner of rectangle
void setSize(int,int);//sets height and width
int getX(void)const;//Return upp left corners' x coordinate
int getY(void)const;//Return upp left corners' y coordinate
int getWidth(void)const;
int getHeight(void)const;
char getPaint(void)const;
void draw(Screen&);//prints contents of rectangle on screen object
};
// implementation class Rectangle
Rectangle::Rectangle()
{
height=1;
width=1;
Paint='*';
}
//paramerized constructor
Rectangle::Rectangle(const Point& object,int h,int w,char c) : upLeft(object),height(h),width(w),Paint(c){}
void Rectangle::set(const Point& object,int h,int w,char c)
{
upLeft=object;
height=h;
width=w;
Paint=c;
}
void Rectangle::setPaint(char e)
{
Paint=e;
}
void Rectangle::setPoint(const Point& object)
{
upLeft=object;
}
void Rectangle::setPoint(int x,int y)
{
upLeft.setPoint(x,y);
}
int Rectangle::getX()const// to check
{
upLeft.getX();
}
int Rectangle::getY()const
{
upLeft.getY();
}
int Rectangle::getWidth()const
{
return width;
}
int Rectangle::getHeight()const
{
return height;
}
char Rectangle::getPaint()const
{
return Paint;
}
void Rectangle::draw( Screen& object)//prints contents of rectangle on screen object
{
int x1=upLeft.getX();
int y1=upLeft.getY();
for(int i=x1;i<(x1+width);i++)
for(int j=y1;j<(y1+height);j++)
object.pixels[x1][y1];
}Perhaps I have not coded properly what Rectangle::draw(void) is supposed to do. Thank you |
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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'; |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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. |
|
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 9:05 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 5:36 AM |
| New to Java - Compiling Error | Kiwwa | Java | 4 | Jun 16th, 2005 10:25 AM |