Hey, I've been getting some very strange results with what seems to be very simple code. All I'm doing is trying to use an STL vector of "cSprite"s -- a class I wrote. The problem arises when I try to resize the vector either explicitly through the "resize" method, or implicitly through the "push_back". Everything compiles fine, but the program crashes upon calling of the methods. If I instead make it a vector of "int"s it can resize, but still crashes upon program exit. I've tried to narrow it down as much as possible, but unfortunately, it's still a bit of code. However, for brevity's sake, I'll only include the class definitions and original usage unless someone would like to see the class contents.
I've been trying to figure this out for days now, and my best guess is just that cSprite has a variable memory footprint, or size. While it may have originally with the cSurface member, it's now a pointer, so this is no longer the case.
Thanks so much for your responses--I've been frustrated with this for a while now.
Original usage: - std::vector<cSprite> vTest(5); // Fine here
-
vTest.resize(vTest.size() + 1); // Crashes on this line
cSprite class definition: - class cSprite
-
{
-
public:
-
cFRect FRect;
-
cFPoint Inertia;
-
cSurface* Surface;
-
int Timer;
-
int Type;
-
-
void Set(int _Type, float _Left, float _Top, float _Right, float _Bottom,
-
float _xInertia, float _yInertia, int _Frame, int _NumberOfFrames,
-
int _TicsPerFrame, bool _Visible, bool _Enabled);
-
void Clear();
-
void Draw();
-
void Move();
-
void Move(float _x, float _y);
-
void Move(cFPoint _cFPoint);
-
void MoveTo(float _x, float _y);
-
void MoveTo(cFPoint _cFPoint);
-
void IncrementFrame();
-
void IncrementFrame(int _Increment);
-
void ResetFrame();
-
int NumberOfFrames();
-
int TicsPerFrame();
-
bool Visible();
-
bool Enabled();
-
-
void setFRect(cFRect _cFRect);
-
void setFRect(float _Left, float _Top, float _Right, float _Bottom);
-
void setFRect(int _Left, int _Top, int _Right, int _Bottom);
-
void setFRect(double _Left, double _Top, double _Right, double _Bottom);
-
void setInertia(cFPoint _cFPoint);
-
void setInertia(float _x, float _y);
-
void setInertia(int _x, int _y);
-
void setInertia(double _x, double _y);
-
void setNumberOfFrames(int _NumberOfFrames);
-
void setTicsPerFrame(int _TicsPerFrame);
-
void Show();
-
void Show(bool _Show);
-
void Hide();
-
void Enable();
-
void Enable(bool _Enable);
-
void Disable();
-
-
cSprite();
-
cSprite(int _Type, float _Left, float _Top, float _Right, float _Bottom,
-
float _xInertia, float _yInertia, int _Frame, int _NumberOfFrames,
-
int _TicsPerFrame, bool _Visible, bool _Enabled);
-
~cSprite();
-
-
private:
-
int iFrame;
-
int iNumberOfFrames;
-
bool bVisible;
-
bool bEnabled;
-
int iFrameTimer;
-
int iTicsPerFrame;
-
cFRect FrameRect;
-
};
cFRect class definition: - class cFRect
-
{
-
public:
-
float Left();
-
float Right();
-
float Top();
-
float Bottom();
-
float Width();
-
float Height();
-
void Move(float _x, float _y);
-
void Move(int _x, int _y);
-
void Move(double _x, double _y);
-
void MoveTo(float _x, float _y);
-
void MoveTo(int _x, int _y);
-
void MoveTo(double _x, double _y);
-
void setFRect(float _Left, float _Top, float _Right, float _Bottom);
-
void setFRect(int _Left, int _Top, int _Right, int _Bottom);
-
void setFRect(double _Left, double _Top, double _Right, double _Bottom);
-
RECT ToRect();
-
cFRect();
-
cFRect(float _Left, float _Top, float _Right, float _Bottom);
-
cFRect(int _Left, int _Top, int _Right, int _Bottom);
-
cFRect(double _Left, double _Top, double _Right, double _Bottom);
-
~cFRect();
-
-
private:
-
float iLeft;
-
float iRight;
-
float iTop;
-
float iBottom;
-
};
cFPoint class definition: - class cFPoint
-
{
-
-
public:
-
float X();
-
float Y();
-
void setX(float _x);
-
void setX(int _x);
-
void setX(double _x);
-
void setY(float _y);
-
void setY(int _y);
-
void setY(double _y);
-
void setPoint(float _x, float _y);
-
void setPoint(int _x, int _y);
-
void setPoint(double _x, double _y);
-
POINTAPI ToPointAPI();
-
cFPoint();
-
cFPoint(float _x, float _y);
-
cFPoint(int _x, int _y);
-
cFPoint(double _x, double _y);
-
~cFPoint();
-
-
private:
-
float x;
-
float y;
-
};
5 6766
What does your default constructor of your cSprite class do?
kind regards,
Jos
My constructors: - cSprite::cSprite()
-
{
-
Type = 0;
-
FRect.setFRect(0, 0, 0, 0);
-
Inertia.setPoint(0, 0);
-
iFrame = 0;
-
iNumberOfFrames = 0;
-
iTicsPerFrame = 0;
-
bVisible = false;
-
bEnabled = false;
-
Surface = NULL;
-
}
-
-
cSprite::cSprite(int _Type, float _Left, float _Top, float _Right, float _Bottom,
-
float _xInertia, float _yInertia, int _Frame, int _NumberOfFrames,
-
int _TicsPerFrame, bool _Visible, bool _Enabled)
-
{
-
Type = _Type;
-
FRect.setFRect(_Left, _Top, _Right, _Bottom);
-
Inertia.setPoint(_xInertia, _yInertia);
-
iFrame = _Frame;
-
iNumberOfFrames = _NumberOfFrames;
-
iTicsPerFrame = _TicsPerFrame;
-
bVisible = _Visible;
-
bEnabled = _Enabled;
-
Surface = NULL;
-
}
You have pointers in yourt class:
cSurface* Surface;
Worse, they are public.
Even worse is no copy constructor.
When you resize a vector, a new vector is created of the wsize and the old vector copied into it. These copies involve call your copy coonstructor, which you don't have. So the compile uses its copy constructor which just copies the pointer. Therefore, the Surface pointer in in the copy is a copy of the pointer in the original. When the destructor is called on the original it scroggs your copy. Then when you use your copy, you crash.
STL requires well-bechaved classes:
1) all appropriate constructors
2) copy coinstructor
3) assignment operator
Try to avoid public data members. They expose your implementation and when these data member names are scattered all over your code, you can't easily change your class. The whole point of a class is to not have public data members.
Wow! Thanks a ton! I've been working on this for a while now and I thought it'd take a while to get a straight answer.
As to the public members thing, yes, I know it's bad practice. I just know I'll be the only one touching the data (no-user specific data) and only so with constants, so error-catching and permissions aren't really an issue. Plus, I only made members public when they were either (A) classes, so they could be passed in their original formats more easily, or (B) intentionally flexible members. By that I mean they're use can vary, so I wouldn't like to restrict them.
Nevertheless, I must confess (though I suppose it's fairly obvious anyway) that I am not the most experienced in writing classes. I would hate ask you to write my code for me, but if there are any good resources you might know of to point me in the right direction (with copy constructors and assignment operators), that would be great!
Again, thank you so much, and I'll be sure to modify the class so I no longer need the pointer.
As to the public members thing, yes, I know it's bad practice. I just know I'll be the only one touching the data (no-user specific data) and only so with constants, so error-catching and permissions aren't really an issue. Plus, I only made members public when they were either (A) classes, so they could be passed in their original formats more easily, or (B) intentionally flexible members. By that I mean they're use can vary, so I wouldn't like to restrict them.
I can't tell you the number of developers who have told me this and then quit theri job leaving the rest of us to deal with the mess.
Do not use public data members unless you can write down a clear techincally sound arguiment and then get an experioenced developer to buy off on it.
Your concerned about flexibility are addressed by using inheritance and polymorphism. Plus a dose of Design Patterns.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jerry Krinock |
last post by:
I've declared a class that has some std::vector data members like this:
class MyClass
{
public:
...
std::vector<Apples> apples ;
...
private:
...
|
by: Stijn Oude Brunink |
last post by:
Hello,
I want to use the vector class to work with arrays of classes but I seem to
get in conflict with the delete operator used in the specific class. The
code below gives an assertion?! How is...
|
by: dalbosco |
last post by:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
|
by: laniik |
last post by:
Hi. For some reason I am getting a crash on pop_back() and Im not sure
why.
sorry I cant post the whole code because the vector is used in a bunch
of places.
i have a
vector<bool> complete;
|
by: He Shiming |
last post by:
Hi,
I've developed a class that implements an interface definition. It looks
like this:
class IRecord {
public:
// define interface methods by pure virtual methods
// no member variables
}
|
by: kathy |
last post by:
I am using std::vector in my program:
func()
{
std::vector <CMyClass *> vpMyClass;
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new...
|
by: ahrimen |
last post by:
Hi, First I'll state my over all goal = a text based game with several
rooms that have several exits as my first real program that I've done
without the help of a book.
I can make a normal...
|
by: Trapulo |
last post by:
Very often my VS2005 pro crashes after a debug.
I run ASP.NET debug, and all works well. Then I stop it, and all it's also
ok. Then, when I try to edit some code, it crashes and I need to kill it,...
|
by: y-man |
last post by:
Hi,
I am creating a child class of the vector, which contains a couple of
functions to make the work with carthesian coordinate vectors easier.
To make things work more easily, I would like to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |