still unsolved. help please

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
  1 #include <iostream>
  2 #include<string>
  3
  4 using namespace std;
  5
  6 class student{
  7
  8 private:
  9     string name;
 10     double height;
 11     int weight;
 12
 13 public:
 14     //constructor
 15     student(string name1, double height1, int weight1)
 16     {
 17         name = name1;
 18         height = height1;
 19         weight = weight1;
 20     }
 21
 22     //Member Methods
 23
 24     void displaystats()
 25     {
 26         cout << name << " " << height << " " << weight;
 27     }
 28
 29 };
 30
 31
 32 int main()
 33 {
 34     string name;
 35     double height;
 36     int n, i, weight;
 37     cin >> n;
 38     student* list[n];
 39
 40
 41     for (i = 0; i < n; i++)
 42     {
 43         cin >> name;
 44         cin >> height;
 45         cin >> weight;
 46         list[i] = new student(name, height, weight);
 47
 48     }
 49
 50
 51     (*(list[2])).displaystats;
 52
 53
 54     return 0;
 55
 56 }


what's wrong with line 51? I am trying to display the name, height and weight of the student that the pointer variable list[2] is pointing to. This is compiling error i get.

bmiclass.cpp:51:30: error: statement cannot resolve address of overloaded function



Last edited on
If you want to call a function, it needs parenthesis:

(*(list[2])).displaystats();

using a non-const variable (n) as the size of the array is also not allowed in C++, so I would avoid doing it even if your compiler supports it.
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
 
 32 int main()
 33 {
 34     string name;
 35     double height;
 36     int n, i, weight;
 37     student** x;
 38     cin >> n;
 39     
 40     x = new student* [n];
 41
 42     for (i = 0; i < n; i++)
 43     {
 44         cin >> name;
 45         cin >> height;
 46         cin >> weight;
 47        
 48         (*x)= new student(name, height, weight);
 49     }
 50
 51
 52     (*(*x)).displaystats();
 53
 54
 55     return 0;
 56
 57 }


----------------------------------------
What syntax should i use on line 48 and 52 to store more elements.

if i change the syntax inside the for loop in line 48 to store 3 student objects

(*x)[i]= new student(name, height, weight);

it doesnt compile.
Last edited on
I think it should be this:

 
x[i] = new student(name, height, weight);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    int n;
    cin >> n;
    student** x = new student* [n];
    for (int i = 0; i < n; ++i)
    {
        string name;
        double height;
        int weight;

        cin >> name;
        cin >> height;
        cin >> weight;

        x[i] = new student(name, height, weight);
    }

    for (int i = 0; i < n; ++i)
        x[i]->displaystats();

    return 0;
}
Topic archived. No new replies allowed.