Having trouble assigning values to a dynamically alocated 2d array of string pointers

So I'm having trouble with assigning values to this array, here's my code:
//Adventurer.cpp

string item = "YEAH";
int rows = 50;
int cols= 2;
int Adventurer::numberOfAdventurers;

Adventurer::Adventurer()
{
cout << "constructor" << endl;

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)

followed by a Backtrace and Memory Map
*** Error in './adventure.out': free(): invalid pointer: (memory adress)

You are calling delete on a pointer that points to an object or objects not created via new or that has already been deleted.

What do your copy constructor and copy assignment operator look like?
// Copy Constructor

Adventurer::Adventurer(const Adventurer& a)
{
rows = a.getCurrentNumberOfItems();
items = new string**[rows];
for( int i = 0; i < rows; i++)
{
items[i] = a.items[i];
}



cout << "copy constructor" << endl;
name = a.name;
maxCarryWeight = a.maxCarryWeight;
currentCarryWeight = a.currentCarryWeight;
currentNumberOfItems = a.currentNumberOfItems;
maxNumberOfItems = a.maxNumberOfItems;
health = a.maxNumberOfItems;
numberOfAdventurers++;
}

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.
Topic archived. No new replies allowed.