I made a class, which need to have 16-17 variables in its private or protected section. Problem is, when I put in more than 14 variables, it compiles okay, but when the class is first used, Windows gives me a "Tester.exe has encountered a problem and needs to close" error. I have clearly linked this to the addition of a fifteenth variable. Any ideas?
class dot{
public:
dot();
dot(fgw::hue const);
dot(int x, int y, int direction, fgw::hue const);
void display(fgw::playpen &);
void fire();
fgw::hue move_forward(fgw::playpen &);
void turn_left(fgw::playpen &);
void turn_right(fgw::playpen &);
void erase(fgw::playpen &);
int x(){ return x_;}
int y(){ return y_;}
void take_a_firing_step(fgw::playpen &);
fgw::hue hit(){return hue_hit;}
bool dead(){ return death;}
void get_eaten();
int dot_direction(){ return direction;}
fgw::hue dot_color(){ return color;}
private:
int direction;
int x_;
int y_;
fgw::hue hue_hit;
fgw::hue color;
bool death;
bool fire_in_process;
bool new_values_locked;
int x1;
int y1;
int last_x;
int last_y;
fgw::hue last_hue;
int firing_direction;
};
#endif
I'm not sure that an example of how I use it would be informative, as it must be interweaved with at least one other class to be useful, however here is an example:
I've slightly changed your code and managed successfully compile and execute it. Addition of 1, 2, 3, ... extra members changes nothing. Indeed, it would be strange if 14 were a magic number... Were it 13 i could believe, but not 14 :)
Interestingly enough, the crash happens when i try to put ten of these into a map storage container. Could i possibly be overloading the maximum storage limit for the container?
EDIT: and agh, why did it put my message all in one line?
No, there is no "storage limit" aside from the amount of continuous space you have on your computer...try printing out "sizeof(dot)" and see how big it is (in bytes)
Ah, sorry, that will make it print out the size of the uninitialized class...make an instance of the class then print out sizeof(<instancename>). (Although I haven't actually ever tried this)
Well, even with creating an instance, it still reads 48. Hmm, and it seems I have left out something. I created an AI for this and the error happens when I put ten of those in to a map. sizeof(<instance of dot_ai>) reads 72.
EDIT: why does the website insist on spelling out my messages on one line?
Hm, well, the only reason I can think of is that you are either out of memory for your program, or you are accessing invalid memory (i.e. memory you don't own). If you can, try stepping through your program and seeing where the variables point...see if they are pointing at bad data (NULL or similar).
Have you looked at the variables/code around that area (look for pointers pointing to bad data, or really big allocations)? I would look at the file but apparently I don't have it...
if you are using map, make sure you have created a proper copy constructor. You don't seem to have one, maybe you are getting an unintentional cast somewhere. Make it a basic rule to always do a copy ctor, default ctor, a dtor and an assignment operator. last but not the least : make sure you are initializing all variables in your ctor.
what key are you using in the map? are you using your object as a key? map has no size restriction but if you are using the object as a key then you may need to provide some more member functions that allow the map to sort your objects since a map is a sorted associative container.
The error isn't in the STL. anders has it exactly:
You must have a copy constructor.
You should probably also have an assignment constructor and a destructor.
(Google c++ the law of the big three)
Failure to copy means that your object will break the instant the map decides to move its memory around. Anything you stick in an STL container should have, at minimum, a copy constructor.