Reputation: 433
I've been trying to convince myself that that objects of the same type have access to each others private data members. I wrote some code that I thought would help me better understand what is going on, but now I am getting an error from XCODE7 (just 1), that says that I am using the undeclared identifier "combination."
If someone could help me understand where I have gone awry with my code, I would love to learn.
My code should simply print false, if running correctly.
#include <iostream>
using std::cout;
using std::endl;
class Shared {
public:
bool combination(Shared& a, Shared& b);
private:
int useless{ 0 };
int limitless{ 1 };
};
bool Shared::combination(Shared& a,Shared& b){
return (a.useless > b.limitless);
}
int main() {
Shared sharedObj1;
Shared sharedObj2;
cout << combination(sharedObj1, sharedObj2) << endl;
return 0;
}
Upvotes: 1
Views: 38
Reputation: 48948
combination
is a member function of the class Shared
. Therefore, it can only be called on an instance of Shared
. When you are calling combination
, you are not specifying which object you are calling it one:
cout << combination(sharedObj1, sharedObj2) << endl;
^^^
Instance?
The compiler complains because it thinks you want to call a function called combination
, but there is none.
So, you'll have to specify an instance:
cout << sharedObj1.combination(sharedObj1, sharedObj2) << endl;
In this case however, it doesn't matter on which instance it is being called on, so you should make combination
static, so you can do
cout << Shared::combination(sharedObj1, sharedObj2) << endl;
Upvotes: 1