所以,我正在尝试用 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
组件.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; }
实体.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(); }