Hello again.
I''m having another problem with the assignement I asked about yesterday.
I have two classes, ShipLoad and Player. Every Player object contains a ShipLoad. Every ShipLoad contains a string array, words, that contain a number of words. Everytime a ShipLoad object is copied the size of the words-array should be increased with 1.
The problem is now that when I add the second word to shipload, I get segmentation error, suggesting that the words-array is not expanded. But that does not seem to be it. If i start using a big words-array, that don't need extending, i still get the same error..
These are the relevant functions:
That's right. It isn't. Arrays can't be resized once they're created.
Why don't you just save yourself the trouble and use an std::vector<std::string>?
No. The copy constructor is only called when an instance is being constructed based on another instance. That's why it's called the copy constructor. For example:
1 2 3
Player johan("Johan");
Player trazan(johan); //Copy constructor being called. The line below is equivalent.
Player trazan=johan;
When you call addWord(), the instance has already been constructed.
Also, the code in the copy constructor looks nothing like what I wrote above.
Ok, what I wan't is for the array words to be expanded with 1 element when the object is copied, thats why I put the array expansion in the copy-constructor. So that after the copy is made, addWord() has room to add one word to the array.
I dont need the temp array, since I have two arrays, this->words and obj.words. I don't need to delete obj.words, since this is done in the destructor.
1 2 3 4 5 6 7
ShipLoad::ShipLoad(const ShipLoad &obj)
{
this->size = (obj.size + 1); //increasing size (which is for the actual size of the array)
this->nrOfWords = obj.nrOfWords; //copying nrOfWords (which is for the actual number of words entered)
words = new string[this->size]; //creating new words array with increased size
this->words = obj.words; //copying old array to new array
}
Is there still something wrong with my reasoning? cause i still get the segmentation error...
Plenty.
Get this into your head: with the code you're using the copy constructor will not EVER be called.
You're not trying to create a new instance of ShipLoad. You're trying to enlarge an array that exists in an instance of ShipLoad that you've already created.
Consider this analogy: You have a fridge, and in it you have a glass of water (A). You need to put more water in your fridge, but you can't add more glasses. Do you buy a new fridge and put a bigger glass in it; or you get a bigger glass (B), pass the contents of A to B, and put B where A was?
This is exactly what you need to do in ShipLoad::addWord(). First, you create a bigger array (get B), copy the contents of the previous array into the new one (put A's water into B), delete the old array (but A in the cupboard), and assign this->words to the new array (put B in the fridge).
But the copy constructor is used. I put a line in it printing to the screen when it is used, and it prints, just before addWord() is called.
I wrote handOverShipload(), so that it should use the copy constructor each time the object shipload is handed over to the next player...
I haven't been following this thread... but yes, that handOverShipLoad function is all wrong.
You're returning a pointer to a temporary object. 'temp2' points to 'temp', but 'temp' is destroyed as soon as the function exists, which means you're returning a pointer to an object that no longer exists. IE: bad pointer.
I didn't notice that before. handOverShipLoad() returns an invalid pointer. temp goes out of scope and is therefore destructed when the function returns.
Returning pointers or references to local objects is illegal.
Even if that wasn't the case, the size of the array is increased in the copy, not in the original (which itself makes no sense. It means the copy constructor doesn't make an exact copy of the parameter passed). You're trying to addWord() to the original, not to the copy.
By the way, what's with the inconsistent use of this->?
Yeah, OK. That explains it. I better think this over once again.
My teacher uses this-> all the time in her constructors, I don't like it, but I used it just to be sure that wasn't the problem. I'm probably going to delete them, it doesn't look nice...