Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to try making classes and objects but it's giving me some errors which I am not able to resolve. Below is my code:


C++
#include<iostream>

using namespace std;

class employee{
    int count;
    int id;
    public:
    void set(void){
        cin>>id;
    }

    void get(void){
        cout<<id;
    }
};
int main(){
    employee shivam;
    shivam.get;
    shivam.set;

return 0;}


It's giving me this error:

PS E:\code practice> cd "e:\code practice\" ; if ($?) { g++ tutt24.cpp -o tutt24 } ; if ($?) { .\tutt24 } 
tutt24.cpp: In function 'int main()':
tutt24.cpp:19:12: error: statement cannot resolve address of overloaded function
   19 |     shivam.get;
      |     ~~~~~~~^~~
tutt24.cpp:20:12: error: statement cannot resolve address of overloaded function
   20 |     shivam.set;
      |     ~~~~~~~^~~
PS E:\code practice> 


What I have tried:

I changed the names of the variables and changed function names, but no change in result.
Posted
Updated 15-Dec-20 10:51am
v2
Comments
jeron1 15-Dec-20 11:02am    
Try changing the get and set calls to shivam.get() and shivam.set().

get and set are functions and need parentheses
 
Share this answer
 
Comments
CPallini 15-Dec-20 16:40pm    
5.
You need to use the correct syntax to access a function:
C++
shivam.get();
shivam.set();

Also it is not generally the best idea to have class functions getting console input. It is better to do that at the main level and then use set to pass the value into the object, and get to return it to the caller.
 
Share this answer
 
Comments
CPallini 15-Dec-20 16:40pm    
5.
As suggested, you could try a more idiomatic approach:
C++
#include<iostream>
using namespace std;

class employee
{
    int id;
    int count;

public:
    employee(int id, int count): id(id), count(count){}

    void set_count(int new_count)
    {
      count = new_count;
    }

    int get_count()
    {
      return count;
    }

    int get_id(void)
    {
      return id;
    }

};


int main()
{
    employee shivam(1,42);
    cout << shivam.get_id() << ", " << shivam.get_count() <<  "\n";
    shivam.set_count(40);
    cout << shivam.get_id() << ", " << shivam.get_count() <<  "\n";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900