items = new string**[rows];
for( int i = 0; i < rows; ++i)
{
items[i] = new string*[cols];
}
for( int i = 0; i < rows; ++i) //The area causing trouble(I think)
{
for( int j = 0; j < cols; ++j)
{
items[i][j] = &item;
cout << *items[i][j] << endl;
}
}
name = "Basic Adventurer";
maxCarryWeight = 150.0;
currentCarryWeight = 0;
currentNumberOfItems = 0;
maxNumberOfItems = 50;
health = 100.0;
numberOfAdventurers++;
}
//Adventurer.h(incomplete: just the private members)
class Adventurer{
private:
string*** items;
string name;
double maxCarryWeight;
double currentCarryWeight;
int currentNumberOfItems;
int maxNumberOfItems;
double health;
static int numberOfAdventurers;
So The values get assigned and output, but at the end i get the error:
*** Error in './adventure.out': free(): invalid pointer: (memory adress)
Adventurer::Adventurer(const Adventurer& a)
{
rows = a.getCurrentNumberOfItems();
items = new string**[rows];
for( int i = 0; i < rows; i++)
{
items[i] = a.items[i];
}
I haven't written my assignment operator yet, could this be the problem? Sorry I also realized this is the wrong thread to post this question(should have posted it on beginner c++
I haven't written my assignment operator yet, could this be the problem?
That would definitely be a problem if it's being used. Of course, your copy constructor is also incorrect so it may be the problem as well. The copy-constructed objects pointed-to arrays belong to the original object. In other words, count the number of times new is invoked in the default constructor and count the number of times new is invoked in the copy constructor - they will not be the same.