Click to See Complete Forum and Search --> : Member function access


April 17th, 1999, 04:21 PM
Hi,

I have two classes : ClassA and ClassB. Now I want a member function
in ClassA to call a member function in ClassB. I tried to make ClassA
a friend of ClassB but compiler compains.

Is there any way around that. Any sample code will highly be appreciated.

Shine B.
April 17th, 1999, 04:43 PM
Show a piece of your code here and people will be of help.

April 18th, 1999, 02:19 PM
// .h file
class test1 {
func1();
func2();
}

class test2 {
friend class test1;
funct2();
funct3();
}

// cpp file

test1::func1()
{
funct1() // I want just this but the vc compiler gives error
// it doesn't know funct1()
}

Dave Lorde
April 19th, 1999, 07:48 AM
The compiler says it doesn't know funct1 because funct1 isn't declared anywhere!

You declare func1 and func2 in test1, and funct2 and funct3 in test2, but no funct1.

If funct1 is supposed to be a member of test2, you must call it on an instance of test2 (unless it is declared 'static'):

class test2 {
friend class test1;
void funct1();
void funct2();
void funct3();
};

test1::func1()
{
test2 anInst; // create an instance of test2
anInst.funct1(); // call the function
}

Incidentally, the class declarations must be terminated with a semi-colon (;) after the curly braces, and so should the call to funct1. The member function declarations should be given return types also.

Dave