我有两个类,foo和bar。 foo包含bar并包含指向条形对象的std :: vector指针。在运行时的某个点,bar必须访问指向其他bar对象的这个向量。因此,foo包含一个名为getBarObjects()的方法,该方法返回指针数组。向前声明与类类型的向量 - 不允许指向不完整类类型的指针
因此,我转发在foo中声明foo。我obvioulsy也必须转发声明我使用的方法 - foo :: getBarObjects()。当它返回指向数组的指针数组时,我陷入了一个恶性循环。
我不能转发include Bar,然后简单地转发include getBarObjects(),因为这会导致“不完整的类型名称是不允许的”。
了foo.h:
#include "bar.h"
#include <vector>
class foo {
public:
foo();
~foo();
std::vector<bar*> getBarObjects();
private:
std::vector<bar*> barObjects;
}
bar.h:
class foo;
std::vector<bar*> foo::getBarObjects(); // error, doesn't know bar at this point
class bar {
public:
bar(foo *currentFoo);
~bar();
bool dosth();
private:
foo *thisFoo;
}
bar.cpp:
#include "bar.h"
bool bar(foo *currentFoo) {
thisFoo = currentFoo;
}
bool bar::dosth() {
thisFoo->getBarObjects(); // error, pointer to inomplete class type is not allowed
}
如果我只是包括周围的其他方式,我将有后来在foo也是一样的问题。有什么建议么?
梅,简单的解决问题似乎很大。谢谢! – Aerius