3

I have two classes A and B. The control is inside one of the member functions of class A. The member function calculates a result and i now want to send this value to one of the member functions of class B, I tried the following way, yet it dint work

 int memberFunctionOfA() { ... //results are stored in some temporary value, say temp B::memberFunctionOfB(temp); // the way i tried } 

The comiler reported an error. I also tried like

 B obj; obj.memberFunctionOfB(temp); 

Both gave me errors that the memberFunctionOfB cannot be called. Can anyone tell me what am i missing

Edit

Class B is not inherited from A. They both are independent. Both the member functions are public and non static

7
  • 3
    Tell us the exact error that the compiler shows. Commented Feb 13, 2011 at 20:16
  • @CHID I don't understand memberFunctionOfB is static function? Commented Feb 13, 2011 at 20:18
  • The declaration of class B would help a lot here. If A inherits from B (or the other way around), class A's declaration would help as well. First guess, i'd say memberFunctionOfB is private/protected, but it's hard to say for sure without seeing the code. Commented Feb 13, 2011 at 20:20
  • 1
    @CHID: That particular error means your class B doesn't have a default constructor. IF you want to create a B, you'll need to supply arguments -- like B obj(42, "These are sample args"); rather than just B obj;. Look through the code for class B and find out what its constructor expects. Commented Feb 13, 2011 at 20:23
  • 1
    compiler said your class B doesn't have default constructor which is required to create object on the stack like you do here: B obj; Commented Feb 13, 2011 at 20:23

2 Answers 2

3

Your second attempt:

int memberFunctionOfA() { ... //results are stored in some temporary value, say temp B obj; obj.memberFunctionOfB(temp); } 

..., looks perfectly valid. We will need the definition of B to help further. B's definition should minimally have, assuming that the member function in B is non-static:

class B { public: void memberFunctionOfB(const TypeOfTemp &temp); }; // Later in class A's definition class A { public: int memberFunctionOfA() { ... //results are stored in some temporary value, say temp B b; b.memberFunctionOfB(temp); } }; 

If the member function in B is static, then this should work:

class B { public: static void memberFunctionOfB(const TypeOfTemp &temp); }; ... class A { public: int memberFunctionOfA() { ... //results are stored in some temporary value, say temp B::memberFunctionOfB(temp); } }; 
Sign up to request clarification or add additional context in comments.

Comments

1

After seeing your comment:

The compiler throws "No matching function call B::B()"

That means, there is no default constructor for class B. In your implementation, B`s constructor must be taking parameter.

So either you add a default constructor to your class, or you pass the argument to your constructor when creating an instance of it.

1 Comment

Thank you nawaz. As they pointed out, i missed the default constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.