Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Oct 27th, 2006, 7:35 PM   #21
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Some of your rectangles are outside of the bounds of screen, which cause the program to write outside the bounds of the array. Note that the rectangle takes its parameters as hieght then width, which is the opposite order to the x,y that point takes.

The values you are passing in for Paint are not printable - char value 1 2 3 and 4 are control characters and will display oddly on your screen. Try using '1', '2', etc.

Inside the loop in the rectangle draw function the point to set is in i and j, not x1 and y1 (my example code is wrong too).
The Dark is offline   Reply With Quote
Old Oct 28th, 2006, 1:33 AM   #22
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
You're right I made all changes and it is ok.
How did you see that the order of the coordinates were inversed?
Now I am working now on another function
Thank You
brad sue is offline   Reply With Quote
Old Oct 28th, 2006, 3:30 AM   #23
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
I only found out that the order was reversed when I ran it and the output looked wrong (and caused an exception when it went out of bounds).
The Dark is offline   Reply With Quote
Old Oct 30th, 2006, 3:58 AM   #24
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
I have separeted my file into file headers and I have an error I can't get rid of :
these ar all my headers files:
// Point.h
#ifndef Point_H
#define Point_H
#include "Rectangle.h"
#include<iostream>
#include"Screen.h"

class Rectangle;
class Point{
      private:
              int x;
              int y;
      public:
             Point();
             Point(int,int);
             ~Point();
             void setPoint(int,int);
             int getX(void) const;
             int getY(void) const;
             };
#endif


//Screen.h
#ifndef Screen_H
#define Screen_H
#include "Rectangle.h"
#include "Point.h"
#include<iostream>


    class Rectangle;
    class Point;
   class Screen
   {
         private:
                 char pixels[20][30];
         public:
                Screen(void);
                ~Screen(void);
                void setPoint(const Point&,char);
                void resetPoint(const Point&);
                void clear(void);
                void draw(void)const;
   };
#endif


// Rectangle.h
#ifndef Rectangle_H
#define Rectangle_H
#include "Point.h"
#include "Screen.h"
#include<iostream>
#include<iomanip>

class Screen;
class Point;
class Rectangle{

          private:
                  int height;
                  int width;
                  Point upLeft;
                  char Paint;
          public:
                 Rectangle(void);
                 Rectangle(const Point&,int,int,char);
                 ~Rectangle();
                 void set(const Point&,int,int,char);rectangle
                 void setPaint(char);
                 void setPoint(const Point&);
                 void setPoint(int, int);
                 void setSize(int,int);
                 int getX(void);
                 int getY(void);
                 int getWidth(void);
                 int getHeight(void);
                 char getPaint(void);
                 void setIntersection( Rectangle,  Rectangle);
                 void setUnion(Rectangle, Rectangle);
                 bool containsPoint( Point);
                 bool containsPoint(int,int);
                 void draw(Screen&);
};                
   #endif

#include<iostream>
#include "Point.h"
#include "Rectangle.h"
#include "Screen.h"
using namespace std;

void check( const Point& pt , char c ,Rectangle r[6]);

int main()
{
   //...
I have this compile error:
compilation: cxx main.cpp Rectangle.cpp Point.cpp Screen.cpp -o main
main.cpp:
cxx: Error: Rectangle.h, line 15: incomplete type is not allowed
                  Point upLeft;
------------------------^
cxx: Info: 1 error detected in the compilation of "main.cpp".
Rectangle.cpp:
Point.cpp:
cxx: Error: Rectangle.h, line 15: incomplete type is not allowed
                  Point upLeft;
------------------------^
cxx: Info: 1 error detected in the compilation of "Point.cpp".
Screen.cpp:
First I would the to fix the error in Rectangle.h

Does someone see what is the problem?
thank you for the help
brad sue is offline   Reply With Quote
Old Oct 30th, 2006, 4:11 AM   #25
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Don't include Screen.h, Rectangle.h or iostream.h inside point.h
You also don't need to forward declare Rectangle inside point.h, as it doesn't use it.

Screen.h doesn't need to include Point.h, Rectangle.h or iostream.h, it just needs the forward declare of Point (or you could just leave the Point.h include in there, but you don't need the include and the forward declare).

Rectangle just needs to include Point.h and have the forward declare of Screen. All the other includes should be removed.

As a rule of thumb, the fewer nested includes you have the better. If the header doesn't need the include, then don't put it in the header.
The Dark is offline   Reply With Quote
Old Oct 30th, 2006, 4:12 AM   #26
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The reason for your error is that a forward declaration of a class is insufficient to allow declaration of an instance. So;
class Point;

class Rectangle
{
    // other stuff
    Point upLeft;
};
will fail. The way to eliminate it is to ensure that the complete declaration of class Point is seen by the compiler before it reaches the declaration of class Rectangle.

Partially related to your problem is that Point.h #include's Rectangle.h which #include's Point.h ..... That will not loop forever because you have employed #include guards, but is an issue because you will get different behaviour for other source files, depending on whether they #include Point.h or Rectangle.h first.
grumpy is offline   Reply With Quote
Old Oct 30th, 2006, 4:17 AM   #27
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Bah, if you are quick editting and someone puts an extra post in, you lose all your changes.

What I was trying to add was pretty much what grumpy said. In this case, only Rectangle needs another actual class (Point) defined for the header to compile.
The Dark is offline   Reply With Quote
Old Oct 30th, 2006, 5:12 PM   #28
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
Thank you, it works!
Thanks for the tip
brad sue is offline   Reply With Quote
Old Nov 1st, 2006, 5:29 AM   #29
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
I am modifying my program now and I want the screen to be a dynamic allocated 2D array.
The code I made compiles well but gives me a run time error:
this is my class
class Screen
   {
         private:
                 char* pixels;
                 int height;//rows
                 int width;//columns
         public: 
                friend ostream& operator<<(ostream & out, const Screen & TV);
                Screen(int=10, int=10);
                ~Screen(void);
                void setH(int );
                int getH(void)const;
                void setW(int);
                int getW() const;
                void setPoint(const Point&,char);
                void resetPoint(const Point&);
                void clear(void);
                void draw(void)const;
   };

// implementation of screen class

   Screen::Screen(int r,int c)
   {
         int size;
         size=r*c;
         pixels=new char[size];
         for(int i=0;i<r;i++)
            {
              for(int j=0;j<c;j++)
                 {
                  *(pixels+(i*c)+j)='.';
                  }
            }
   }             
      
      
   Screen::~Screen(){delete []pixels;}
   
   void Screen::setPoint(const Point & object, char c)
   {
        int i=object.getX();
        int j=object.getY();        
        *(pixels+(i*width)+j)=c;
   }          
                 
   void Screen::draw()const
   {
        for( int i=0;i<height;i++)
            {for( int j=0;j<width;j++)
                 {cout<<*(pixels+(i*width)+j);}
                 cout<<endl;
                 }
   }    
   
   void Screen::resetPoint(const Point& object)
   {
        int x_del,y_del;
        x_del=object.getX();
        y_del=object.getY();
        *(pixels+(x_del*width)+y_del)='.';
   }   
   
   void Screen::clear()
   { int i,j;
         for(i=0;i<height;i++){
             for(j=0;j<width;j++){
                 *(pixels+(i*width)+j)='.';}}
   }
    
    void Screen::setH(int row)
    {height=row;}
    
    int Screen::getH()const{ return height;}
    
    void Screen::setW(int col)
    {         width=col;}
    
    int Screen::getW() const{ return width;}
    
         
    ostream& operator<<(ostream & out, const Screen & TV)
    { 
             for(int i=0;i<TV.height;i++)
                 for(int j=0;j<TV.width;j++)
                    out<<*(TV.pixels+(i*TV.width)+j);
                    out<<endl;
    }
    
int main()
{
    Screen ecran(20,30);
    cout<<"\n\nPrinting a blank Screen"<<endl;
    ecran.draw();// or cout ecran; still gives error

I am supposed to have a 20X30 table but what is displayed is more like:

..............................................
...............................................
...............................................
..............................................
(8 lines only!)
and the error message
Please, can some help me to debug it?
Thank you.
brad sue is offline   Reply With Quote
Old Nov 1st, 2006, 6:33 AM   #30
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
You are not setting the values of height and width in your constructor.
The Dark 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 9:44 PM
Masm rsnd Assembly 4 May 20th, 2006 10:05 PM
libraries matko C 1 Jan 22nd, 2006 3:12 PM
HELP please!!! hamacacolgante C 7 Nov 21st, 2005 6:36 AM
New to Java - Compiling Error Kiwwa Java 4 Jun 16th, 2005 11:25 AM




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

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