New to classes and hitting an object error

Hello, I am new to using classes and I'm not 100% sure if I'm doing this right, and I don't understand why I'm getting the error:
37:24: error: cannot call member function 'void stats::push(int)' without object
39:20: error: cannot call member function 'void stats::print()' without object
(compiling on g++)

Can anybody please explain what this means in relevance to my code? (I tried google to no avail :\)

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
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>


class stats {
public:
  int sum, min, max;
  std::vector<int> V_num;
  void push(int);
  void print();
};

void stats::push(int numin){
 stats input;
 V_num.push_back(numin);

 input.min = *std::min_element(V_num.begin(), V_num.end());
 input.max = *std::max_element(V_num.begin(), V_num.end());
 input.sum = std::accumulate(V_num.begin(), V_num.end(), 0);
}

void stats::print(){
  std::cout << "N    = " << V_num.size();
  std::cout << "\nSum = " << sum;
  std::cout << "\nMin = " << min;
  std::cout << "\nMax = " << max << std::endl;
}



int main(void){
int numin;

while(std::cin >> numin){ //while loop to get all cin and push into vector.
      stats::push(numin);
     }
      stats::print();

    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int main(void){
    stats stat ;

    int numin ;  // Why would you make this global?
    while(std::cin >> numin)
        stat.push(numin);
     
    stat.print();

    return 0;
}
The error is because you cannot call member functions that are not part of an instantiated object.

1
2
3
4
5
6
7
8
9
10
int main()
{
       stats stat; //Instantiate a stats object.
       int numin = 0;
       while(std::cin >> numin)
              stat.push(numin); //Push numin into stat
       
       stat.print(); //stat.print(); not stats::print()
       return 0;
}


Pretty much just what cire said.
Topic archived. No new replies allowed.