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.