【发布时间】:2016-12-24 09:35:24
【问题描述】:
我目前正在使用 SFML 和 C++ 开发一个小游戏,但我遇到了一个问题。我在 character.h 中有一个 Character 类,里面有 2 个函数,但是当我尝试在另一个文件 (Game.cpp) 中访问这些函数时,一个可以完美运行,而另一个则好像它甚至不存在一样。由于这是我的第一篇文章,我不知道如何正确展示我的代码,所以如果我不够清楚,请告诉我。 谢谢大家,祝你有美好的一天。
/****CHARACTER.H****/
#ifndef CHARACTER_H
#define CHARACTER_H
#include <SFML/Graphics.hpp>
using namespace std;
class Character{
public:
Character();
~Character();
void initPlayer(string& fileName, sf::IntRect rect);
void moveCharacter();
sf::Sprite m_sprite;
private:
sf::VertexArray m_vertices;
sf::Texture m_texture;
};
#endif
/****CHARACTER.CPP*****/
#include "/home/hichem/C++/sfml/Game Engine/character.h"
#include "string"
#include <iostream>
using namespace std;
Character::Character(){
}
Character::~Character(){
}
void Character::initPlayer(string& fileName, sf::IntRect rect){
if (!m_texture.loadFromFile(fileName, rect)){
cout << "failed to load image" << endl;
}
m_sprite.setTexture(m_texture);
m_sprite.setPosition(sf::Vector2f(400, 200));
}
void Character::moveCharacter(){
}
/****GAME.H****/
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include "character.h"
#include "tileMap.h"
#include "string"
class Game: public sf::Transformable{
public:
Game();
~Game();
void run(sf::RenderWindow &window);
private:
void event(sf::RenderWindow &window);
void update();
void draw(sf::RenderWindow &window);
TileMap carte;
Character player;
const int level_1[128] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
};
#endif // GAME_H
/****GAME.CPP****/
#include "Game.h"
using namespace std;
Game::Game(){
string a = "images/tiles.png";
carte.load(a, sf::Vector2u(50, 50), level_1, 16, 8);
string b = "images/player.png";
/*FUNCTION THAT WORKS FROM CHARACTER.H*/
player.initPlayer(b, sf::IntRect(0,0,50,50));
}
Game::~Game(){
//dtor
}
void Game::event(sf::RenderWindow &window){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
/*FUNCTION THAT DOES NOT WORKS FROM CHARACTER.H*/
player.moveCharacter();
}
}
void Game::update(){
}
void Game::draw(sf::RenderWindow &window){
window.clear();
window.draw(carte);
window.draw(player.m_sprite);
window.display();
}
void Game::run(sf::RenderWindow &window){
while(window.isOpen()){
event(window);
update();
draw(window);
}
}
【问题讨论】:
-
那么问题是什么?不是在编译吗?还是什么都不做?
-
将您得到的任何编译错误或错误输出复制为 text 并粘贴到问题的正文中。
-
在你的 character.h 头文件的顶部(在 include-guard 内)放置一个 #error 并确保在编译时 character.cpp 正在拉入你认为的头文件。它看起来不像,从
#includehyjinx/differences 判断,您似乎也可能怀疑。您的错误不是标记线。原始错误在 character.cpp 中,表示您的成员不存在。显然它确实存在于您在此处提供的标题中。 -
您包含一个错误的 .h 文件。检查路径。不要包含在绝对路径中。
-
你应该避免
using namespace std;,尤其是在头文件中。