Calling a private class help?

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
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

class addClass{

    public:
    void setInt (int x)
    {
            add2 = x;
    }
    int getInt(){
    return add2;
    }



     private:
     int add2 (int a,int b)

    {
    int ans = a + b;
    cin >> a;
    cin >> b;
    ans = a + b;
    return ans;
    }

};



int main()
{
    addClass addOB;
   addOB.setInt(0,0);
   cout << addOb.getInt();


    system ("Pause");

    return 0;
}







can anyone tell what im missing to make this private/ public class transfer properly? is there a specific "set/get" definition i need to know for dealing with integers/numbers?

Last edited on
void setInt (int x)

addOB.setInt(0,0);

The way you called SetInt function is invalid as there is an extra argument in your function call.

1
2
3
4
5
6
7
int add2 (int a,int b)
{
    int ans = a + b;
    cin >> a;
    cin >> b;
    ans = a + b;
}


This function needs a return which you havent included.

k i edited it to "return ans;" and changed the int to void in the public class because its going to only be printing out.
setInt takes one argument so passing two arguments as you do on line 34 is an error.

Inside setInt you assign a value to add2. add2 is a function so that doesn't make sense. If you want the class to have a member variable named add2 you have to add it to the class definition.
1
2
3
4
5
6
class addClass
{
	...
	int add2;
	...
};

You can't have a member variable with the same name as a member function so if you do with you will have to rename or remove the add2 function.
Topic archived. No new replies allowed.