2012-07-11 57 views
-3

我正在創建一個非常簡單的基於組件的遊戲引擎。我有一個存儲要遍歷的組件列表的實體類。存儲一個指向類的實例的指針(這不起作用)

實體類還存儲信息,如名稱,位置,規模無論什麼。我需要的是每個組件存儲對擁有它的實體實例的引用。我最初嘗試使用「this」關鍵字,但它不起作用,因爲您不能在作業中使用它。

void Entity::addComponent(Component *theComponent){ 
    components.push_back(theComponent); 
    theComponent->ownerEntity = this; 
} 

組件如何存儲指向其所有者的指針?

感謝您的幫助!

編輯:組件類中有幾乎毫無作爲其旨在繼承,但這裏是它的聲明:

class Entity; 

class Component 
{ 
    public: 
     Component(); 
     virtual void Update(); 
     Entity *ownerEntity; 
    protected: 
    private: 

}; 

當製作一個組件,如果我試着訪問所有者實體,像這樣:

rotation = ownerEntity->GetRotation; 

我得到這個錯誤:

error: argument of type 'float (Entity::)()' does not match 'float' 
+0

顯示組件類請 – Andrew 2012-07-11 13:49:09

+2

如果您對所有權建模,請不*使用原始指針。使用智能指針。 – 2012-07-11 13:50:01

+3

「我最初嘗試使用」this「關鍵字,但它不起作用,因爲您無法在作業中使用它。」我不知道你有什麼想法。使用'this'作爲賦值的右側沒有任何錯誤。你需要更好地描述你面臨的問題,因爲目前的描述是非問題的描述。 – 2012-07-11 13:51:46

回答

1
rotation = ownerEntity->GetRotation; 

應該是:

rotation = ownerEntity->GetRotation(); 

調用的函數。

+0

謝謝,我覺得很愚蠢 – Constan7ine 2012-07-11 13:56:29

+0

我們都犯錯誤。下次你真的很快就會發現這一個我敢打賭! – Flexo 2012-07-11 13:57:07

相關問題