![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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). |
|
|
|
|
|
#22 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#23 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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).
|
|
|
|
|
|
#24 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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()
{
//...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:Does someone see what is the problem? thank you for the help |
|
|
|
|
|
#25 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#26 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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;
};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. |
|
|
|
|
|
#27 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#28 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Thank you, it works!
Thanks for the tip |
|
|
|
|
|
#29 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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 errorI am supposed to have a 20X30 table but what is displayed is more like: .............................................. ............................................... ............................................... .............................................. (8 lines only!) Please, can some help me to debug it? Thank you. |
|
|
|
|
|
#30 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
You are not setting the values of height and width in your constructor.
|
|
|
|
![]() |
| 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 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 |