如果得到一個很簡單的問題(至少我希望如此),但我不知道該怎麼做才能告訴g ++以什麼順序「完成」我的類。我它降低到這個簡單的例子:gcc:交叉引用類編譯
base.h:
#ifndef BASE_H_
#define BASE_H_
#include "otherclass.h"
class otherclass;
class base {
public:
base(otherclass* other);
protected:
otherclass* m_other;
};
#endif /* BASE_H_ */
derived.h:
#ifndef DERIVED_H_
#define DERIVED_H_
#include "base.h"
class derived : public base {
public:
derived(otherclass* other);
};
#endif /* DERIVED_H_ */
(二者對應cpp文件僅包含構造,其分配給定otherclass *到成員變量)
otherclass.h:
#ifndef OTHERCLASS_H_
#define OTHERCLASS_H_
#include "derived.h"
class otherclass {
public:
otherclass();
protected:
derived* m_derived;
};
#endif /* OTHERCLASS_H_ */
otherclass.cpp:
#include "otherclass.h"
otherclass::otherclass() {
m_derived = new derived(this);
}
因爲它可能是相關的,我會貼scons的整個輸出:
g++ -o base.o -c base.cpp
g++ -o derived.o -c derived.cpp
In file included from otherclass.h:4:0,
from base.h:4,
from base.cpp:1:
derived.h:8:29: error: expected class-name before '{' token
g++ -o main.o -c main.cpp
In file included from base.h:4:0,
from derived.h:4,
from derived.cpp:1:
otherclass.h:11:3: error: 'derived' does not name a type
所以它看起來像基地是一個未知的類型,在這一點上與gcc並且預先聲明它顯然使它哭泣,因爲從一個不完整的類型派生是被禁止的。
我真的沒有看到問題在這裏(當然除了錯誤信息)。
有人能夠啓發我嗎?