user4396386 user4396386 - 1 year ago 66
C++ Question

Do functions need to be declared anywhere else besides in the class and before the program?

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 {
bool combination(Shared& a, Shared& b);
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;

Answer Source

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;

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download