だから、私は 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(); }