Compare two values from two classes

Mar 18, 2015 at 5:54am
Hi guys, First and most important, I thank you for your time and knowledge. I don't want any code, just need an explanation, or to know what I should read and study, so I can learn how to compare the values from two classes at once please. I can't find this anywhere, I'm certain I'm not looking under the proper terminology.

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
#include <iostream>
using namespace std;
class a {
private:
    int variable;
public:
    void Set_variable(int _a){
        variable=_a;
    }
    int Get_variable(){
        return variable;
    }
};
class b {
private:
    int number;
public:
    void Set_number(int _a){
        number= _a;
    }
    int Get_number(){
        return number;
    }
};
void compare() {
    int tmp1, tmp2, tmp3;
// tmp1=a.obj->Get_variable();  // error primary expression before "."
// tmp1=a.Get_variable();       // error primary expression before "."
// tmp1=a::Get_variable();      // error cannot call member function 'int a::Get_variable()' without object|
// int tmp2=val::Get_number();
    if (tmp1==tmp2)
    {
        cout << "Alright! " << endl;
    }
    else cout << "Dang it." << endl;
}
int main()
{
    a obj;
    b val;
    obj.Set_variable(5);
    val.Set_number(10);
    compare();
    return 0;
};
Last edited on Mar 18, 2015 at 5:59am
Mar 18, 2015 at 6:13am
Your variables obj and val are declared in main(). You try to access them in compare() but they aren't passed in the parameters. That leads to an error.

1
2
3
4
5
6
7
void compare(a &obj, b &val) //passing by reference
{
    int tmp1, tmp2;
    tmp1 = obj.Get_variable();
    tmp2 = val.Get_number();
    //rest of your code
}
Topic archived. No new replies allowed.