Trouble with arrays of pointers (EDIT: now trouble with StringStream!)

Hi all,

I'm hoping somebody can help me out with confusion about pointers. I have a class "Person" with constructor Person(string name) and a getName() method which returns the name. As an exercise (it's not assessed; I'm not trying to cheat), I have to create an array of Person pointers, add Person objects (named simply "Person 0", "Person 1" etc) to be pointed at by the various pointers, and then print the names of each Person via the pointers.

The main method I've written is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char** argv)
{
    //get the desired number of people for the array
    cout << "How many people? ";
    int numberPeople;
    cin >> numberPeople;

    //create the array of Person pointers
    Person** personPointersArray = new Person*[numberPeople];

    //cycle through the array, adding a person to each slot
    for(int index = 0; index < numberPeople; index++)
    {
        stringstream personName;

        //create a name for the person at that index (e.g. "Person 1", "Person 2" etc)
        personName << "Person " << index;

        *(personPointersArray + index) = new Person(personName.str()); //create a person with that name, and assign its address to the pointer
        cout << (personPointersArray + index)->getName(); //Print name of the person at that index
    }
}


The compiler rejects the final line of the for-loop, citing this error:

request for member ‘getName’ in ‘*(personPointersArray + ((long unsigned int)(((long unsigned int)index) * 8ul)))’, which is of non-class type ‘Person*’

Can anyone much smarter than me find where I've gone wrong? Would be greatly appreciated.
Last edited on
personPointersArray is a 'Person**'
therefore
(personPointersArray + index) is also a 'Person**'
therefore
(personPointersArray + index)->getName(); is an error because you're using -> to dereference a Person** (instead of a Person*, as you intended).

You need to dereference the Person** to get a Person*. Then you dereference the Person* with the -> to get the Person.

The hard way to do this (which seems to be what you like doing) is this:
 
cout << (*(personPointersArray + index))->getName();


The easy way is to just use the [] operator:
 
cout << personPointerArray[index]->GetName();


I don't know why you are so afraid of the [] operator in your code, but you really could simplify lines 19,20:

1
2
        personPointersArray[index] = new Person(personName.str()); //create a person with that name, and assign its address to the pointer
        cout << personPointersArray[index]->getName(); //Print name of the person at that index 



Remember that *(x+y) is the exact same thing as x[y]. Only x[y] is much easier to read/understand and is less prone to the above confusion.
Last edited on
Understood. I think I was confused as to how the [] operator dereferences the pointer.
Thanks for your assistance!
The previous problem was solved; however, the names of the created Persons are still not being printed. I've replaced the final line of the above code with:

cout << personPointersArray[index]->getName() << endl;

But instead of getting each name on a separate line, I simply get blank lines corresponding to the number of Persons.
I tested the constructor of the Person class, to ensure it was setting the name correctly. It was.
I then changed the code so that each Person object was created with the same name, "jeff", in case it was the stringstream's fault. It worked properly, printing out "jeff" however many times.

From the code above, can anyone see anything wrong with how I've used the stringstream? I've tried to follow my textbook but obviously I'm doing it wrong...
No I don't see anything wrong with that...Maybe your constructor(s) are incorrectly initializing the name?
Slept on it, looked at it this morning and realised it was the constructor's fault. Wasn't accepting numbers! Always the simple things, eh?

Thanks to both of you for your help.
Topic archived. No new replies allowed.