【发布时间】:2012-10-09 01:03:18
【问题描述】:
所以,我正在尝试用 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_ */
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(); }
【问题讨论】:
-
您是否尝试过在
Entity.h中前向声明Component类,而不是像在Component.h中使用Entity那样包含头文件? -
@JoachimPileborg 刚试过。同样的问题
-
从错误消息中还不清楚哪个文件正在编译,但我只是尝试使用 g++ 4.3.3 和 MSVC++ 9 编译 Component.cpp 和 Entity.cpp(在注释掉
Entity::~Entity()和Entity::update()使用autoC++11-ism) 并且他们都愉快地编译它而没有错误。所以我确定错误不是来自这些文件。 -
在编译每个 .cpp 文件时应该使用 -c,然后在单独的步骤中将它们链接在一起,或者应该省略 -c 并在单个命令行上指定所有 .cpp 文件。
-
@j_random_hacker 我想通了! Eclipse 报告的“错误”实际上是代码分析问题。令人惊讶的是,开发人员认为默认情况下将它们与编译器错误放在同一类别中是个好主意。幸运的是,我能够将它们自定义为仅显示为警告。 :) 另外,您能否发布一个简单的答案以便我接受?
标签: c++ g++ dependencies header-files circular-dependency