2012-10-18 29 views
0

所以,我想在C++中開發一個非常簡單的遊戲(我總是使用C#,我只是潛入C++),並且想要複製我的簡單(雖然設計不佳)我用C#製作的組件實體系統。循環依賴兩類 - 代碼不編譯

此代碼不能使用g ++與C++ 11標準進行編譯。

我該如何解決?我是否必須更改設計,或者是否有解決方法?


好格式化pastie:http://pastie.org/5078993


Eclipse的錯誤日誌

Description Resource Path Location Type 
Invalid arguments ' 
Candidates are: 
void push_back(Component * const &) 
' Entity.cpp /TestEntity line 15 Semantic Error 
Invalid arguments ' 
Candidates are: 
__gnu_cxx::__normal_iterator<Component * *,std::vector<Component *,std::allocator<Component *>>> erase(__gnu_cxx::__normal_iterator<Component * *,std::vector<Component *,std::allocator<Component *>>>) 
__gnu_cxx::__normal_iterator<Component * *,std::vector<Component *,std::allocator<Component *>>> erase(__gnu_cxx::__normal_iterator<Component * *,std::vector<Component *,std::allocator<Component *>>>, __gnu_cxx::__normal_iterator<Component * *,std::vector<Component *,std::allocator<Component *>>>) 
' Entity.cpp /TestEntity line 19 Semantic Error 
Method 'update' could not be resolved Entity.cpp /TestEntity line 22 Semantic Error 
Invalid arguments ' 
Candidates are: 
#0 remove(#0, #0, const #1 &) 
' Entity.cpp /TestEntity line 19 Semantic Error 

Component.h

#ifndef COMPONENT_H_ 
#define COMPONENT_H_ 

class Entity; 

class Component { 
private: 
    Entity* parentPtr; 

public: 
    virtual void init(); 
    virtual void update(); 
    virtual ~Component(); 
    void setParent(Entity* mParentPtr); 
}; 

#endif /* COMPONENT_H_ */ 

Component.cpp

#include "Component.h" 

void Component::setParent(Entity* mParentPtr) { parentPtr = mParentPtr; } 

Entity.h

#ifndef ENTITY_H_ 
#define ENTITY_H_ 

#include "Component.h" 

class Entity { 
private: 
    std::vector<Component*> componentPtrs; 

public: 
    ~Entity(); 
    void addComponent(Component* mComponentPtr); 
    void delComponent(Component* mComponentPtr); 
    void update(); 
}; 

#endif /* ENTITY_H_ */ 

Entity.cpp

#include <iostream> 
#include <vector> 
#include <list> 
#include <string> 
#include <sstream> 
#include <algorithm> 

#include "Entity.h" 
#include "Component.h" 

Entity::~Entity() { 
    for (auto &componentPtr : componentPtrs) delete componentPtr; 
} 
void Entity::addComponent(Component* mComponentPtr) { 
    componentPtrs.push_back(mComponentPtr); 
    mComponentPtr->setParent(this); 
} 
void Entity::delComponent(Component* mComponentPtr) { 
    componentPtrs.erase(remove(componentPtrs.begin(), componentPtrs.end(), mComponentPtr), componentPtrs.end()); 
    delete mComponentPtr; 
} 
void Entity::update() { for (auto &componentPtr : componentPtrs) componentPtr->update(); } 
+0

您是否嘗試過'Entity.h'中的'Component'類的前向聲明,而不是包含頭文件,就像您使用'Component.h'中的'Entity'一樣? –

+0

@JoachimPileborg剛剛嘗試過。同樣的問題 –

+0

從錯誤信息中我們還不清楚哪些文件甚至被編譯,但我只是試着用g ++ 4.3.3和MSVC++ 9編譯Component.cpp和Entity.cpp(在註釋掉Entity ::〜 Entity()'和'Entity :: update()'它們使用'auto' C++ 11-ism),並且它們都能夠愉快地進行編譯而不會出錯。所以我確定錯誤不是來自這些文件。 –

回答

1

事實證明,實際上沒有循環依賴問題,並且代碼編譯和鏈接正常。 Eclipse的代碼分析報告實際上只是過分熱衷的 - 編譯每一個。CPP在命令行上單獨文件,

g++ -c thatfile.cpp 

不會產生編譯錯誤,並編譯和一步到位的

g++ Entity.cpp Component.cpp 

連接生產工作的可執行文件。

+0

再次感謝!我還在Eclipse CDT BugZilla頁面上提交了一個錯誤/建議報告。 –

0

您必須包括Entity.hcomponent.cpp

+0

我試過這樣做,但同樣的錯誤生成。 –

0

是否有可能,這個問題不是圓形包括但代碼本身?例如:「Method'update'could not be resolved」給你一個提示,你必須定義Component::update方法,與Component::init-方法相同,因爲它們不是純虛擬的。嘗試先刪除這個錯誤,然後看看是否有幫助

更新: 所以...我測試了你的代碼。不是在eclipse中,而是在Visual Studio 2008中。我用for (auto...替換爲「normal」for (std::vector<Component*>::iterator...,因爲這個版本的VS中沒有這個功能。正如我所能說的那樣,沒有循環依賴,但是在Entity.h文件中忘記了#include <vector>。並且這些虛擬功能的定義丟失了,但是你說過,你已經添加了它們,所以忘記了這一點;)

+0

我可以通過評論這些方法使這個錯誤消失。但「無效參數」錯誤仍然存​​在 –