1

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

5
  • You'll have to provide the error too if you want help. Commented Aug 17, 2010 at 10:22
  • 1
    Too many syntax errors: class declarations need to be followed by a ;, functions must have a return type (e.g. int for main) etc. Commented Aug 17, 2010 at 10:26
  • Ignore the syntax error. Tell me is it possible to execute Commented Aug 17, 2010 at 10:43
  • How about fixing the syntax error so we don't have to? Commented Aug 17, 2010 at 10:50
  • It is quite possible, but without the real error being shown its hard to know which particular problem the compiler is choking on... the Error that you have added is not the error, but a note printed by the compiler after the real error was printed. Provide the errors from the first one. (And also note that the missing ; can choke the compiler and make it misinterpret the rest) Commented Aug 17, 2010 at 10:53

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

another problem is, that the class B and class C must be declared before class A.
2

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..

Comments

1

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) .

1 Comment

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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.