![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
I made the changes, and I also change the overload operator and draw so that it display correctly in the monitor.
I succeded to have to correct form ( the 20X30 table) but again a run time error: Ichange the 2 entity like this: 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;}
}
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;}// i changed the position of the'}' here and in draw().
}debugging run time error is new I found them difficult to catch. what is the deal with those? B. Last edited by brad sue; Nov 1st, 2006 at 10:12 AM. |
|
|
|
|
|
#32 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
When I run it, I only get the run time error when a rectangle tries to write outside the screen. Then I get a runtime when it tries to free the screen.
Can you show your changed code? |
|
|
|
|
|
#33 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
I did disable all commands of other functions.
Here is my Screen 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(const Screen&);
~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;
height=r;
width=c;
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::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;}
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;}
}
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;}
}
for the moment my main is:
int main()
{
Screen ecran(20,30);
cout<<"\n\nPrinting a blank Screen"<<endl;
cout<<ecran<<endl;
return 0;
}It is the only changes I made beside the the two I did show above. |
|
|
|
|
|
#34 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
I don't know if it causes the problem, but you are not returning the stream in your << function.
The compiler should have warned you about this problem. |
|
|
|
|
|
#35 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
thank you !Yes that was the issue .
I am struggling with a function that reduces the size of the screen by a factor x. The problem is that I dont succeed to keep the location of the painted point s on the screen canvas. the redcution is done by deleting the remaining points I coded this:: void Screen::shrink(int reduc)
{
if((reduc<height)&&(reduc<width))
{ height=height/reduc;
width=width/reduc;
for( int i=height;i<0;i--)
for( int j=width;j<0;j--)
delete[](pixels+(i*width)+i);
}//end if
}if the screen is: 4455*888 4455*888 5555*888 ******* if we do shrink(2), the screen should be: 4455 4455 In my function the size it OK but the location of the points is not kept. because I don't know how to 'jump' one line up to start deleting again. I continue to work on it but Please if someone has a suggestion please send it to me. B. |
|
|
|
|
|
#36 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
Since you allocted pixels as one block of char, it is a very bad idea to try to delete[] the chars individually. You either can leave the block at its original size (and have some unused memory) or allocate a new block, copy the contents you want across and then delete the old block.
As for copying the data across to the new size, you need to keep track of the old size and use that to find the location of the pixels you want to copy from the old data. |
|
|
|
|
|
#37 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just an additional caveat, though it's implicit in The Dark's post: don't forget that your destructor is going to delete 'pixels', so be wary of how you mess with that.
__________________
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 |
|
|
|
|
|
#38 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
I think it would be better to change my constructor and destructor as allocate memory individually. I will try to do this first then tackle the 'shrink' again.
thank you for the suggestions. |
|
|
|
|
|
#39 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
I tried to change my constructor but it fails.Then I tried this
void Screen::shrink(int reduc)
{
if((reduc<height)&&(reduc<width))
{
int newrow,newcol;
newrow=height/reduc;
newcol=width/reduc;
char *newpixels=new char[newrow*newcol];
//iterate the old block line by line
for ( int i = 0; i < newrow; i++)
{
memcpy( newpixels + (i*newcol), pixels + (i*width), newcol); //copy only the required amount of data
}
//delete the old data
delete []pixels;
pixels = newpixels;
}
}See where I am wrong in my code? |
|
|
|
|
|
#40 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
Same problem as in post #29, which was answered in post #30
|
|
|
|
![]() |
| 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 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 |