Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 23rd, 2006, 5:19 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
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.
brad sue is offline   Reply With Quote
Old Oct 23rd, 2006, 8:27 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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).
The Dark is offline   Reply With Quote
Old Oct 26th, 2006, 8:31 AM   #3
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by The Dark View Post
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.
brad sue is offline   Reply With Quote
Old Oct 26th, 2006, 8:50 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 26th, 2006, 5:33 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by brad sue View Post
`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.
The Dark is offline   Reply With Quote
Old Oct 26th, 2006, 9:15 PM   #6
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by The Dark View Post
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
brad sue is offline   Reply With Quote
Old Oct 26th, 2006, 9:27 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Oct 27th, 2006, 1:10 AM   #8
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by The Dark View Post
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.
brad sue is offline   Reply With Quote
Old Oct 27th, 2006, 1:45 AM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Oct 27th, 2006, 1:57 AM   #10
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
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?
brad sue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC