still giving error

0 Alam Khattak · January 4, 2015
/images/forum/upload/2015-01-04/33a1104c23c309a3147d4e652692337f.jpgg error

Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · January 4, 2015
You can't call a function without () and you also can't output it without an object cause the function is part of the object.

Your main is supposed to look like this:
int main(){
  ComputerRoom AlamsObject;
  cout << AlamsObject.addNumbers(36, 21);
}



But I would recommand not to use a function with an return value. Because than you don't even need the class you could just make a simple function which adds two numbers.

You should make a private variable in the class where you save the result.
And than another function to access  this variable.

e.g.:
#include <iostream>
using namespace std;

class ComputerRoom{
  private:
     int answer;
  public:
     void addNumbers(int x, int y){
        answer = x + y;
     ]
     int getanswer(){
        return answer;
     }
};

int main(){
  ComputerRoom AlamsObject;
  AlamsObject.addNumbers(36, 21);
  cout << AlamsObject.getanswer();
}


The advantage of this is that you can get the value of the addition everytime in the programm and don't need to calculate it again if you need it.
0 Aju Antony · January 4, 2015
edit 15th and 16th line to the following

15 cout<<AlamsObject.addNumbers(36,21);

the error is because you are referencing a member function of a class directly without the use of an object.

even though you are returning the answer at 15th line, you are not storing it in any variable and then in the 16th line you are accessing the member function of class directly. 
  • 1

C++

107,340 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator