hello,
I want to make a little program with a friend method but there is a bug.
In my example code, I have a class A which have the method f from clas B as friend.
files:
A.h
#ifndef _A_H
#define _A_H
#include "B.h"
class A {
public:
A();
friend void B::f(A);
private:
int x;
};
#endif /* _A_H */
A.cpp
#include "A.h"
A::A() {
x=10;
}
B.h
#ifndef _B_H
#define _B_H
class A;
class B {
public:
B();
void f(A);
private:
};
#endif /* _B_H */
B.cpp
#include "B.h"
#include <iostream>
using namespace std;
void B::f(A a){
cout << a.x;
}
B::B() {
}
int main(int argc, char argv[]){
A ex;
f(ex);
return 0;
}
here is the issue:
B.cpp: In member function `void B::f(A)':
B.cpp:12: error: `a' has incomplete type
B.h:10: error: forward declaration of `struct A'
B.cpp:13: error: invalid use of undefined type `struct A'
B.h:10: error: forward declaration of `struct A'
B.cpp: In function `int main(int, char*)':
B.cpp:21: error: aggregate `A ex' has incomplete type and cannot be defined
B.cpp:22: error: `f' undeclared (first use this function)
B.cpp:22: error: (Each undeclared identifier is reported only once for each function it appears in.)
I searched in some tutorials and in some books but in vain.
can you help me?
olivier.