LNK 2001未解决C ++ SFML问题

时间:2014-03-06 21:40:54

标签: c++ visual-studio-2010 sfml

我正在尝试使用SFML库在C ++中创建一个在屏幕上移动的角色。 每次创建项目时,我都会遇到相同的LNK 2001错误。

请向我解释我做错了什么以及如何解决它。我还是初学者,所以如果你有任何关于修复错误或编写代码的建议,我会喜欢它。

Main.cpp的:

#include "Game.cpp"
#include "Game.h"

int main ()
{
    Game Game;

    Game.GameRun();
}

Game.h:

#pragma once

#include <SFML\Window.hpp>
#include "Character.h"

class Game
{
public:
    void GameRun ();
    void UpdateGame(Character player);
    void Controls(int sourceX, int sourceY, Character player);
};

Character.h:

#pragma once

#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include "Game.h"

class Character
{
public:
    enum Direction {Down, Left, Right, Up};

    int characterX;
    int characterY;

    sf::Texture txt_character;
    sf::Sprite spr_character;
};

Game.cpp:

#include "Game.h"
#include "Character.h"

#include <iostream>

inline void Game::GameRun()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML test");

    Character player;

    player.characterX = 1;
    player.characterY = Character::Down;

    player.txt_character.loadFromFile("character sprite sheet.png");
    player.spr_character.setTexture(player.txt_character);

    while(window.isOpen())
    {
        Game::UpdateGame(player);
    }
}

inline void Game::UpdateGame(Character player)
{
    sf::Event event;
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML test");

    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        window.close();
    }
    Game::Controls(player.characterX, player.characterY, player);

    player.spr_character.setTextureRect(sf::IntRect(player.characterX * 32, player.characterY * 32, 32, 32));

    window.draw(player.spr_character);
    window.display();
    window.clear();
}

inline void Game::Controls(int sourceX, int sourceY, Character player)
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        sourceY = Character::Up;
        sourceX++;
        if (sourceX * 32 >= player.txt_character.getSize().x)
            sourceX = 0;
        player.spr_character.move(0, -2);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        sourceY = Character::Down;
        sourceX++;
        if (sourceX * 32 >= player.txt_character.getSize().x)
            sourceX = 0;
        player.spr_character.move(0, 2);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        sourceY = Character::Left;
        sourceX++;
        if (sourceX * 32 >= player.txt_character.getSize().x)
            sourceX = 0;
        player.spr_character.move(-2, 0);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        sourceY = Character::Right;
        sourceX++;
        if (sourceX * 32 >= player.txt_character.getSize().x)
            sourceX = 0;
        player.spr_character.move(2, 0);
    }
}

错误:

1&gt; main.obj:错误LNK2001:未解析的外部符号“public:static int Character :: characterY”(?characterY @ Character @@ 2HA)

1&gt; main.obj:错误LNK2001:未解析的外部符号“public:static int Character :: characterX”(?characterX @ Character @@ 2HA)

1&gt; C:\ Users \ SONY \ Documents \ Visual Studio 2010 \ Projects \ Top Down Game \ Debug \ Game.exe:致命错误LNK1120:2个未解析的外部

谢谢:D

编辑:仅供参考我有没有类的代码,它工作得很好,它只是main.cpp中的一大块代码

编辑:指向程序文件夹的链接:https://www.dropbox.com/s/3yyjti8zwu019s7/Top%20Down%20Game%20With%20Classes.rar

1 个答案:

答案 0 :(得分:2)

编辑2014/03/08:我尝试过您的来源。删除#include "Game.cpp"文件中的Main.cppinline文件中的Game.cpp关键字。

背后的逻辑不起作用所以我将其修改为最小值,以便在修复编译(链接)错误后立即生效。您应该尝试我的代码,看看它是否有用。

先解释一下

按值传递播放器对象(如同void someMethod(Character player)时那样)将对象的副本发送给方法。因此,当您对播放器对象进行更改时,这些更改仅对副本而不是实际播放器进行。我改变了你的代码,所以它发送了一个指针(void someMethod(Character * player)。该方法接收一个指向实际玩家对象的指针,对该对象所做的更改反映在实际的玩家对象上。

除了使用指针之外还有一些解决方案(有些人可能会认为它太过分或过于危险)。

可能是:

  • 参考void someMethod(Character& player)
  • 或返回值Character someMethod(Character player)
  • 或两个Character someMethod(const Character& player)
  • 的混合
  • 或成员变量(在头文件中定义)

另一件事是你在Character类中按值保留sf :: Texture。 sf :: Texture实际上是“重”对象,所以它们不应该通过值传递,而应该作为指针保存。你可以做的最好的事情(我在下面的代码中没做过)是从Character类中完全删除sf :: Texture并且只保留已经拥有sf :: Texture的sf :: Sprite(在一种优化的方式,所以你不用担心它。)

这是代码

<强> Main.cpp的

#include "Game.h"

int main ()
{
    Game Game;

    Game.GameRun();
}

<强> Game.h

#ifndef GAME_H_
#define GAME_H_

//#include <SFML/Window.hpp> /* you don't need this */
#include <SFML/Graphics/RenderWindow.hpp>


// forward declaration, this prevents recursive/circular dependencies.
// only works with pointer or referenced type.
class Character;

class Game
{
public:
    void GameRun ();

    // I change the param to pointer to Character for logic reasons.
    // You were doing changes on a Character COPY of the player,
    // which was deleted when leaving the scope of the functions.
    void UpdateGame(Character * player);
    void Controls(Character * player);
private:
    // I added this here, so you can create the window in the 
    // GameRun and UpdateGame methods where needed.
    sf::RenderWindow window;
};

#endif

<强> Game.cpp

#include "Game.h"
#include "Character.h"

#include <SFML/Window/Event.hpp> /* you only need this */

#include <iostream>

void Game::GameRun()
{
    // I moved the window creation here because you were 
    // creating a new window each update which made the windows
    // flickers each frame, making it impossible to close properly.
    window.create(sf::VideoMode(800, 600), "SFML test");

    Character * player = new Character();

    player->characterX = 1;
    player->characterY = Character::Down;

    // if the file isn't available, this will prevent further problems.
    if (player->txt_character.loadFromFile("character.png"))
    {
        player->spr_character.setTexture(player->txt_character);
    }

    while (window.isOpen())
    {
        Game::UpdateGame(player);
    }
}

void Game::UpdateGame(Character * player)
{
    sf::Event event;


    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed){

            window.close();

        }
    }
    Controls(player);

//  player.spr_character.setTextureRect(
//          sf::IntRect(player.characterX * 32, player.characterY * 32, 32,
//                  32));

    window.clear();
    window.draw(player->spr_character);
    window.display();

}

// You were already sending the player so I removed the other
// params and used the player instead.
void Game::Controls(Character * player)
{
    int sourceX = player->characterX;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        player->characterY = Character::Up;
        sourceX++;
        if (sourceX * 32 >= player->txt_character.getSize().x)
            sourceX = 0;
        player->spr_character.move(0, -2);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        player->characterY = Character::Down;
        sourceX++;
        if (sourceX * 32 >= player->txt_character.getSize().x)
            sourceX = 0;
        player->spr_character.move(0, 2);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        player->characterY = Character::Left;
        sourceX++;
        if (sourceX * 32 >= player->txt_character.getSize().x)
            sourceX = 0;
        player->spr_character.move(-2, 0);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        player->characterY = Character::Right;
        sourceX++;
        if (sourceX * 32 >= player->txt_character.getSize().x)
            sourceX = 0;
        player->spr_character.move(2, 0);
    }
    player->characterX = sourceX;
}

<强> Character.h

#ifndef CHARACTER_H_
#define CHARACTER_H_

//#include <SFML\Graphics.hpp>
//#include <SFML\System.hpp>
//#include <SFML\Window.hpp>
//#include "Game.h"
#include <SFML/Graphics/Sprite.hpp> /* Don't include more than you need */
#include <SFML/Graphics/Texture.hpp>

class Character
{
public:
    enum Direction {Down, Left, Right, Up};

    int characterX;
    int characterY;

    sf::Texture txt_character; // Don't keep the texture value, it's really heavy.
    sf::Sprite spr_character;
};

#endif

常规C ++信息