【发布时间】:2017-07-11 08:17:26
【问题描述】:
我在 PlayerController.h 中收到 C2504 编译错误,指出我的基类(可更新)未定义。我已经搜索了几个小时来寻找具有继承问题的循环包含的解决方案,他们的解决方案是删除循环包含并使用前向声明。据我了解,如果没有调用前向声明类中的方法,则此方法有效。但是,在我的程序中,我的 Updateables 类调用其成员继承的 gameObject 对象的方法,而 GameObjects 也调用其成员 Updateables 的方法。因此,Updateables 需要包含 GameObject.h,而 GameObjects 需要包含 Updateables.h。这导致 PlayerController.h 中的 C2504 表示找不到基类 Updateable。
这是我的相关课程:
组件.h
#pragma once
#include "Vector3.h"
class GameObject;
class Component {
public:
GameObject* gameObject = nullptr;
Component();
};
组件.cpp
#include "Component.h"
Component::Component() {}
可更新.h
#pragma once
#include "Component.h"
#include "GameObject.h"
class GameObject;
class Updateable : public Component {
public:
~Updateable();
virtual void update() = 0;
};
可更新的.cpp
#include "Updateable.h"
Updateable::~Updateable() {
if (gameObject) {
gameObject->removeUpdateable(this);
}
}
GameObject.h
#pragma once
#include "Updateable.h"
#include "GameManager.h"
class Updateable;
class GameObject {
public:
GameObject();
~GameObject();
void runUpdateables();
void addUpdateable(Updateable* updateable);
void removeUpdateable(Updateable* updateable);
private:
vector<Updateable*> updateables;
};
游戏对象.cpp
#include "GameObject.h"
GameObject::GameObject() {
updateables = vector<Updateable*>();
GameManager::addGameObject(this);
}
GameObject::~GameObject() {
GameManager::removeGameObject(this);
}
void GameObject::runUpdateables() {
for (unsigned int i = 0; i < updateables.size(); i++) {
updateables[i]->update();
}
}
void GameObject::addUpdateable(Updateable* updateable) {
updateables.push_back(updateable);
updateable->gameObject = this;
}
void GameObject::removeUpdateable(Updateable* updateable) {
auto it = find(updateables.begin(), updateables.end(), updateable);
if (it != updateables.end()) {
updateables.erase(it);
}
}
PlayerController.h
#pragma once
#include "Updateable.h"
//#include "GameObject.h"
#include "Input.h"
class Updateable;
class PlayerController : public Updateable {
public:
float speed = 5.0f;
void update();
};
PlayerController.cpp
#include "PlayerController.h"
void PlayerController::update() {
float x = 0;
if (Input::getKeyDown(GLFW_KEY_A)) {
x = -speed;
}
if (Input::getKeyDown(GLFW_KEY_D)) {
x = speed;
}
cout << x << endl;
gameObject->getRigidBody()->velocity.x = x;
//yes this is a method in GameObject that I removed from this post
//because it would take up more space, rigidbody.h does not create
//a circular dependency
}
GameManager.h
#pragma once
#include "GameObject.h"
#include "PlayerController.h"
class GameManager {
public:
static void init();
static void addGameObject(GameObject* go);
static void removeGameObject(GameObject* go);
static void onFrame();
private:
static vector<GameObject*> gameObjects;
static GameObject* box;
GameManager.cpp
#include "GameManager.h"
vector<GameObject*> GameManager::gameObjects;
GameObject* GameManager::box;
void GameManager::init() {
gameObjects = vector<GameObject*>();
box = new GameObject();
box->addUpdateable(new PlayerController());
}
void GameManager::addGameObject(GameObject* go) {
gameObjects.push_back(go);
}
void GameManager::removeGameObject(GameObject* go) {
auto it = find(gameObjects.begin(), gameObjects.end(), go);
if (it != gameObjects.end()) {
gameObjects.erase(it);
}
}
void GameManager::onFrame() {
for (unsigned int i = 0; i < gameObjects.size(); i++) {
gameObjects[i]->runUpdateables();
}
}
这是确切的错误消息:错误 C2504 'Updateable': base class undefined Basic Platformer c:\users\default.sixcore-pc\documents\visual studio 2015\projects\basic platformer\basic platformer\playercontroller.h 9
【问题讨论】:
-
为什么在
Updateable中转发声明PlayerController? -
你确实使用了
Component,你在Updatable中使用它,所以你需要定义它的构造函数。 -
您需要在使用对象的源文件中包含必要的头文件。在具有正确前向声明的头文件中,您根本不需要包含任何头文件。
-
@RickAstley 我在尝试寻找解决方案时不小心把它留在了那里,我没想到它会起作用,它没有
-
@Galik 我添加了我的component.cpp,它有一个构造函数,但里面什么都没有,所以我最初没有添加它
标签: c++ inheritance include composition circular-dependency