Trouble with dynamic arrays, structures and functions

I'm having some trouble with a dynamically created struct array, in that when i try to use it to copy its concents to another dynamic array so that i can return the temporary dynamic array's value to the main function, it gives me an error.

"main.cpp|18|error: request for member 'name' in '*(values + ((sizetype)(((unsigned int)i) * 4u)))', which is of pointer type 'person*' (maybe you meant to use '->' ?)|" this is the error i get, below is the full code. the partss that deserve attention are in line 18, 19 and 48.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <iostream>
#include <string>

using namespace std;

struct person
{
    string name;
    int number;
};

person *growArray (person *values[], int *size)
{
    *size *= 2;
    person *temp = new person[*size];
    for (int i = 0; i < *size; i++)
    {
        temp[i].name = values[i].name; // here is where i have problems
        temp[i].number = values[i].number;
    }
    delete [] values;
    return temp;
}


person addFriend (int index, int size)
{
    person *temp = new person[size];
    cout << "\nPlease enter the name of your friend: ";
    cin >> temp[index].name;
    cout << "Please enter last date spoken to: ";
    cin >> temp[index].number;
    return temp[index];

}

//int updateValue (int *array[], int index)

int main()
{
    int size = 10;
    person *list = new person[size];
    cout << "Welcome to Person Manager 2014, the game where you pretend to have friends and die!!!\n\n";
    for (int i = 0; i < size + 1;)
    {
        if (i + 1 == size)
        {
            list = growArray (list, size);
        }
        int answer;
        cout << "What would you like to do?\n1)Input new friends\n2)Update last time talked\n3)Sort by name\n4)Sort by name\n";
        cout << "\nOption number: ";
        cin >> answer;
        if (answer == 1)
        {
           list[i] = addFriend (i, size);
            i++;
        }
        else if (answer == 2)
        {
            //string answer2; (part of the code is comented out to test things)
            //list[i].number = updateNumber;
        }
        else if (answer == 3)
        {
          //  displayNumber (list);
        }
        else if (answer == 4)
        {
            //displayName (list);
        }
        else if (answer > 4 || answer < 0)
        {
            cout << "Invalid number, choose again\n\n";
        }
    }
}


i'm not sure what i'm doing wrong there. i'm afraid i can't make my question more specific than "what am i doing wrong", "why is it wrong" and "how can it be fixed".
Last edited on
I think you need to read up on pointers.

1
2
3
4
5
6
7
8
9
10
11
struct person
{
    string name;
}

void someFunction()
{
    person *bob = new person;
    bob->name = "Bob"; //this is correct
    bob.name = "Bob"; //this is incorrect
}


In the example, bob is a pointer to a person, not a person itself. So you need to use the -> operator to access its elements.

If you look at line 12 of your code, you are passing values as a pointer to a dynamic array to your function. In the function itself, temp is a dynamic array. They are different types.
Last edited on
addFriend() is leaking memory like crazy. You're storing objects of person in your list, so you're going to make a copy anyway, there's no point faffing about with pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
person addFriend ()
{
    person temp;
    cout << "\nPlease enter the name of your friend: ";
    cin >> temp.name;
    cout << "Please enter last date spoken to: ";
    cin >> temp.number;
    return temp;
}
...
int main()
{
...
    if (1 == answer)
    {
        list[i++] = addFriend();
    }


Things would be different if list was a collection of pointers.

The growArray() is another problem, the parameter is an array of pointers, but of course what we have and pass is an array of objects. We also pass size, an int where a pointer is required.

By doubling size before the loop we'll end up trying to copy twice as much data as we have, thereby accessing out of bounds data.

With these changes it should at least compile and run, and watch out for istream errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>

using namespace std;

struct person
{
    string name;
    int number;
};

person *growArray (person *values, int *size)
{
    person *temp = new person[2 * (*size)];
    for (int i = 0; i < *size; i++)
    {
        temp[i].name = values[i].name; // here is where i have problems
        temp[i].number = values[i].number;
    }
	
    (*size) *= 2;
    delete [] values;
    return (temp);
}

person addFriend ()
{
   person temp;
    cout << "\nPlease enter the name of your friend: ";
    cin >> temp.name;
    cout << "Please enter last date spoken to: ";
    cin >> temp.number;
    return temp;
}

//int updateValue (int *array[], int index)

int main()
{
    int size = 2;
    person *list = new person[size];
    cout << "Welcome to Person Manager 2014, the game where you pretend to have friends and die!!!\n\n";
    for (int i = 0; i < size;)
    {
		if ((i + 1) >= size)
		{
			list = growArray (list, &size);
		}
		
        int answer;
        cout << "What would you like to do?\n1)Input new friends\n2)Update last time talked\n3)Sort by name\n4)Sort by name\n";
        cout << "\nOption number: ";
        cin >> answer;
        if (answer == 1)
        {
			list[i++] = addFriend ();
        }
        else if (answer == 2)
        {
            //string answer2; (part of the code is comented out to test things)
            //list[i].number = updateNumber;
        }
        else if (answer == 3)
        {
          //  displayNumber (list);
        }
        else if (answer == 4)
        {
            //displayName (list);
        }
        else if (answer > 4 || answer < 0)
        {
            cout << "Invalid number, choose again\n\n";
        }
    }
}


In main, because the loop termination is " i < size" ( I changed that from "i < size + 1") and size doubles when i == size + 1, size is a moving target i can never approach. Whenever a user selects an option, they will enter an infinite loop. You need to make one of the options a choice to break out of the loop.
Topic archived. No new replies allowed.