Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   compiling error (http://www.programmingforums.org/showthread.php?t=11673)

brad sue Oct 23rd, 2006 5:19 PM

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 3

I have the following errors:
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.

The Dark Oct 23rd, 2006 8:27 PM

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).

brad sue Oct 26th, 2006 8:31 AM

Quote:

Originally Posted by The Dark (Post 117357)
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).

I tried what you have suggested me . Now
:

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];

      }

the compiler gives me those errors:

:

`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.

DaWei Oct 26th, 2006 8:50 AM

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.

The Dark Oct 26th, 2006 5:33 PM

Quote:

Originally Posted by brad sue (Post 117581)
:

`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.

That is what "private" means! It means only the Screen class can access the pixels member variable. Making Rectangle a friend class in Screen should get rid of the error, but may not be the best way to do things. As DaWei says, you need to post some more code.

Also note that
:

object.pixels[x1][y1];
Doesn't actually do anything.

brad sue Oct 26th, 2006 9:15 PM

Quote:

Originally Posted by The Dark (Post 117642)
That is what "private" means! It means only the Screen class can access the pixels member variable. Making Rectangle a friend class in Screen should get rid of the error, but may not be the best way to do things. As DaWei says, you need to post some more code.

Also note that
:

object.pixels[x1][y1];
Doesn't actually do anything.

Here is more code. This is the order how I defined the classes Screen and Rectangle in one file.

:


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];
               
      }

Note the two draw functions in rectangle and screen classes.
Perhaps I have not coded properly what Rectangle::draw(void) is supposed to do.
Thank you

The Dark Oct 26th, 2006 9:27 PM

If you want class Rectangle to be a friend class to Screen, put
:

friend class Rectangle;
inside screen.
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;
before the Screen class declaration.

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';
Is setting the pixel to the constant 'c', not the value of the c parameter.

brad sue Oct 27th, 2006 1:10 AM

Quote:

Originally Posted by The Dark (Post 117668)
If you want class Rectangle to be a friend class to Screen, put
:

friend class Rectangle;
inside screen.
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;
before the Screen class declaration.

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';
Is setting the pixel to the constant 'c', not the value of the c parameter.

Thanks Dark,
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.

The Dark Oct 27th, 2006 1:45 AM

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.

brad sue Oct 27th, 2006 1:57 AM

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