(netbeans) problème de méthode amie
bonjour,
je voudrais essayer de faire un petit problème avec une méthode amie mais ça plante.
dans l'exemple, il y a une classe A qui a la méthode f de la classe B pour amie.
fichiers:
A.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #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
Code:
1 2 3 4 5 6
| #include "A.h"
A::A() {
x=10;
} |
B.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #ifndef _B_H
#define _B_H
class A;
class B {
public:
B();
void f(A);
private:
};
#endif /* _B_H */ |
B.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #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;
} |
voici l'erreur:
Citation:
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.)
j'ai cherché dans des tutos de developpez.com ainsi que dans des livres que j'ai mais sans succès.
savez-vous comment modifier mon programme pour qu'il marche?
olivier.