因此,我一直在尝试列出我的项目(GameObject)中的类的指针列表。我尝试使用GameObject **,列表和矢量来做到这一点。但是,每一个都会产生很多错误。以下是相关文件:
Globals.h:
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include "GameObject.h"
#include <vector>
#define TITLE "Belty McBelth"
#define WIDTH 1280
#define HEIGHT 720
extern sf::RenderWindow window;
extern sf::Font font;
extern sf::Event ev;
extern sf::Texture playerTex;
extern sf::Sprite playerSprite;
extern sf::Texture tieTex;
extern sf::Sprite tieSprite;
extern std::vector<GameObject *> ObjectList;
extern sf::Clock deltaClock; //A clock that measures the deltaTime(time
since last frame) in seconds
extern float deltaTime; //Time since last frame in seconds
int setup();
void CleanUp();
void Exit();
void CenterOrigin(sf::Sprite & t);
GlobalDefinitions.cpp:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "GameObject.h"
#include <vector>
std::vector<GameObject*> ObjectList;
sf::RenderWindow window;
sf::Font font;
sf::Event ev;
sf::Texture playerTex;
sf::Sprite playerSprite;
sf::Texture tieTex;
sf::Sprite tieSprite;
sf::Clock deltaClock; //A clock that measures the deltaTime(time since last
frame) in seconds
float deltaTime; //Time since last frame in seconds
Globals.cpp:
#include "Globals.h"
int setup()
{
window.create(sf::VideoMode(WIDTH, HEIGHT), "Belty McBelth",
sf::Style::None);
if (!font.loadFromFile("../comic.ttf"))return 1;
if (!playerTex.loadFromFile("../PLAYER.png"))return 1;
playerSprite = sf::Sprite(playerTex);
std::cout << "Setup completed without errors \n";
return 0;
}
void CenterOrigin(sf::Sprite & t)
{
sf::FloatRect d = t.getLocalBounds();
t.setOrigin(d.width / 2, d.height / 2);
}
void CleanUp()
{
for (GameObject* g : ObjectList)delete g;
}
void Exit()
{
CleanUp();
window.close();
}
GameObject.h:
#pragma once
#include "Globals.h"
enum ID
{
player, tie, powerup
};
class GameObject
{
const char * name;
const ID id;
sf::Sprite sprite;
sf::Vector2f position;
public:
GameObject(const char* n, ID i, sf::Sprite spr, sf::Vector2f pos)
:name(n), id(i), sprite(spr), position(pos) {}
virtual void Update();
virtual void Draw() const;
virtual void Draw(sf::Shader&)const;
const char* getName() const;
const ID getID() const;
sf::Vector2f getPosition() const;
void ChangeTexture(sf::Texture&);
void SetPosition(const sf::Vector2f);
void Move(float, float);
void Move(const sf::Vector2f);
template<typename T> static GameObject* Instantiate(const char* n, ID i,
sf::Sprite spr, sf::Vector2f pos);
};
template<typename T>
inline GameObject * GameObject::Instantiate(const char* n, ID i, sf::Sprite
spr, sf::Vector2f pos)
{
GameObject* obj = new T();
obj->name = n;
obj->id = i;
obj->sprite = spr;
obj->position = pos;
ObjectList.push_back(obj);
return obj;
}
GameObject.cpp:
#include "GameObject.h"
void GameObject::Update()
{
sprite.setPosition(position);
Draw();
}
void GameObject::Draw() const
{
window.draw(sprite);
}
void GameObject::Draw(sf::Shader& s) const
{
window.draw(sprite, &s);
}
const char * GameObject::getName() const
{
return name;
}
const ID GameObject::getID() const
{
return id;
}
sf::Vector2f GameObject::getPosition() const
{
return position;
}
void GameObject::ChangeTexture(sf::Texture & t)
{
sprite.setTexture(t);
}
void GameObject::SetPosition(const sf::Vector2f p)
{
position = p;
}
void GameObject::Move(float xOff, float yOff)
{
position.x += xOff;
position.y += yOff;
}
void GameObject::Move(sf::Vector2f v)
{
position += v;
}
在构建前,Visual Studio无法识别任何错误,但是当我尝试构建项目时,会遇到很多构建错误。在阅读了这些错误的Microsoft页面之后,我无法找到发生这些错误的原因。
答案 0 :(得分:2)
我认为您在Globals.h
和GameObject.h
之间有一个循环包含关系。我建议您仅在GameObject
中转发声明Globals.h
,而不包括GameObject.h
。之所以可以这样做,是因为您仅使用GameObject
作为指针。
在这里circular dependency
Globals.h
//#include "GameObject.h"
externs...
class GameObject;
std::vector<GameObject*> ObjectList;
GameObject.h
#include "Globals.h"
class GameObject{...};