0

だから、私は C++ で本当に単純なゲームを開発しようとしています (私は常に C# を使用していましたが、C++ に飛び込んでいます)、C# で作成した単純な (ただし設計が不十分ですが) コンポーネントエンティティシステムを複製したいと考えていました。

このコードは、C++11 標準で g++ を使用してコンパイルされません。

どうすれば修正できますか?デザインを変更する必要がありますか、それとも回避策はありますか?


適切にフォーマットされたパスティ: 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_ */

コンポーネント.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_ */

エンティティ.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(); }
4

3 に答える 3

1

実際には循環依存の問題はなく、コードは正常にコンパイルおよびリンクされていることがわかりました。「エラー」は、実際には Eclipse による熱狂的なコード分析レポートです。コマンド ラインで各 .cpp ファイルを個別にコンパイルします。

g++ -c thatfile.cpp

コンパイルエラーは発生せず、コンパイルとリンクを一度に実行できます

g++ Entity.cpp Component.cpp

動作する実行可能ファイルを生成します。

于 2012-10-18T18:59:03.587 に答える
0

問題が循環インクルードではなく、コード自体にある可能性はありますか? 例: 「メソッド 'update' を解決できませんでした」は、メソッドを定義する必要があることを示しています。メソッドは純粋な仮想ではないためComponent::update、-method と同じです。Component::init最初にこのエラーを削除してから、これが役立つかどうかを確認してください

更新: だから...私はあなたのコードをテストしました。Eclipse ではなく、Visual Studio 2008 を使用します。これは、このバージョンの VS では利用できなかったため、for (auto..."normal"に置き換えました。for (std::vector<Component*>::iterator...私が言うことができるように、循環依存関係はありませんが#include <vector>、Entity.h ファイルを忘れました。そして、それらの仮想関数の定義が欠落していましたが、あなたはそれらを既に追加したと言ったので、それを忘れてください;)

于 2012-10-18T14:45:41.963 に答える
0

に含める必要がありEntity.hますcomponent.cpp

于 2012-10-18T14:27:29.470 に答える