tags:

views:

90

answers:

5

Hai

I have a class A,B,C now i have member function in class A which needs the objects of class B,C. Is it possible to achieve. If yes how?

class A 
{
  public:

  void fun(B obj_b,C obj_c)
  {

   }

} 

class B 
{
  //some work
} 

class C
{
  //some work
}


int main()
{
  B obj_b;
  C  obj_c;
  A obj_a;

  obj_a.fun(obj_b,obj_c); //I GET ERROR HERE DURING COMPILATION  
}

What is wrong in the above code?.

ERROR:

note: synthesized method first required here error: initializing argument 1 of ‘void A::fun(B,C)’

Thank you

+3  A: 

If it's not a typo : Your function fun is ill-formed, the return type is missing.

void fun(B obj_b,C obj_c) // void was missing
  {

   }

And as other pointed, there are several other problem with this code as missing return type for main, missing ; for class definitions, class B and class C must be declared before class A...

This last one seems to be what your error says.

Klaim
another problem is, that the `class B` and `class C` _must_ be declared before `class A`.
Yossarian
Thanks, added to the answer.
Klaim
A: 

You must make the fun(B obj_b,C obj_c) as the friend funcion to both Class B and C.

jmaniac
eh, what? that function is public, and needs to be visible only for main.
Yossarian
+2  A: 

Might be because of class declaration..

At void fun(B obj_b,C obj_c) the compiler has no idea about class B and class C. So try giving like

class B 
{
  //some work
}; 

class C
{
  //some work
};

class A 
{
  public:

  void fun(B obj_b,C obj_c)
  {

   }

};

If the classes are in different header files, #include them in class A..

Hope it helps..

liaK
Thank's a lot this solved the problem
kar
+1  A: 

The classes B and C are unknown in the void fun(B obj_b,C obj_c) line, you have to declare them first.

Either declare and define them first, if this is not possible (e.g. because they also need A), you can use forward declarations. It's just two single lines

class B;
class C;

BTW: With the method declaration void fun(B obj_b,C obj_c) you pass the parameters per value, what means that the objects B and C are copied. Most of the time this is not what u want: Pass them by reference by changing the method signature to void fun(B& obj_b,C& obj_c) .

IanH
If the objects are **passed by value** you **cannot forward declare** them. Also in referenced objects, only **if you are not accessing the referenced object's members**, you can forward declare them.
liaK
A: 

After you correct all the syntax errors, you may encounter an error for the line declaring the fun member function, saying that the classes B and C are not yet declared. Place a forward declaration for classes B and C before the A class declaration. That is, something like:

class B;  
class C;  
class A  
{  
public:
void fun(B obj_b, C obj_c)
... other stuff here
};
class B
{
... real declaration of the class B
};
class C
{
... real declaration of the class C
};

By the way, forward declarations are the only method of circularly-referenced class declarations.

mbaitoff