Pattern strategy et inclusions réciproques
Bonjour,
J'ai une classe A avec tout un tas d'attributs. Dans la classe A je créé un pointeur vers la classe B, la classe B a pour but d'effectuer un algorithme prenant en argument tous les attributs de A (design strategy :?).
Ma classe B possède une méthode qui prends en argument une référence constante sur un objet A.
main.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream>
#include "b.h"
#include "a.h"
int main()
{
A a;
a.sayHello();
return 0;
} |
a.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #ifndef A_H
#define A_H
#include "b.h"
class A
{
public:
A():m_sMessage("Message cree dans le constructeur") { m_pB = new B; }
const std::string& getMessage() const { return m_sMessage; }
void sayHello() { m_pB->sayHello(*this); }
private:
B* m_pB;
std::string m_sMessage;
};
#endif |
b.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef B_H
#define B_H
#include "a.h"
#include <iostream>
class B
{
public:
void sayHello(const A& a) { std::cout << a.getMessage() << std::endl; }
};
#endif |
J'ai un problème d'inclusion des fichiers (j'imagine). Le compilateur me crache:
Code:
1 2 3 4 5 6 7 8 9 10
| In file included from b.h:4,
from main.cpp:3:
a.h:13: error: ISO C++ forbids declaration of B with no type
a.h:13: error: expected ; before * token
a.h: In constructor A::A():
a.h:9: error: m_pB was not declared in this scope
a.h:9: error: expected type-specifier before B
a.h:9: error: expected `;' before B
a.h: In member function void A::sayHello():
a.h:11: error: m_pB was not declared in this scope |
Que faire??
Merci