c++ - Getting 2 ERROR Messeges Eclipse compiler: non static member and error 2 -


i new c++, have been doing tasks training. task make calculation while using class , accessing private integer.

here full code.

#include <iostream> using namespace std;  class calculatour{  public:  int sumnum(int a, int b){      cin >> a;     cin >> b;      x = a+b;      return x; }     private:  int x;  };   int main() {  calculatour add;  cout << add.sumnum;  return 0; } 

i have been getting error on line:

cout << add.sumnum; 

where says

reference non-static member function must called add calculatour using classes.cpp /add calculatour using classes/src line 37 c/c++ problem.

also have been getting error too:

make: *** [src/add calculatour using classes.o] error 1 add calculatour using classes c/c++ problem

please consider new language. if can provide solution , explanation helpful.

thanks

when invoking method (or calling function) parameters 1 must supply parameters if not used. since in method have no intention of using parameters , have no class hierarchy forces include these parameters, may discard them.

#include <iostream> using namespace std;  class calculatour { public:     int sumnum()     {         int a;         int b;         cin >> a;         cin >> b;          x = + b;          return x;     }  private:     int x; };  int main() {     calculatour add;     cout << add.sumnum();     return 0; } 

the more ideologically correct solution (the calculator class should calculator things, not data in/out things) read , b in in main, , call sumnum.

#include <iostream> using namespace std;  class calculatour { public:     int sumnum(int a, int b)     {         x = + b;         return x;     }     int sumnum(int a) // takes advantage of stored x value     {         x += a;         return x;     } private:     int x = 0; };  int main() {     calculatour add;     int a;     int b;      cin >> a;     cin >> b;      cout << add.sumnum(a, b);     cout << add.sumnum(a);     return 0; } 

there no way compiler tell

int sumnum() {     int a;     int b;     cin >> a;     cin >> b;      x = + b;      return x; } 

from

int sumnum() {     int a;     cin >> a;      x += a;      return x; } 

so cannot take advantage of overloading , having same method name perform different tasks different input.

say want

double sumnum() {     double a;     double b;     cin >> a;     cin >> b;      return + b; } 

to take floating point input. can't. you'd have change method's name or use templates.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -