c++ - 使用 C++ SFML Unresolved LNK 2001

标签 c++ visual-studio-2010 sfml

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

请向我解释我做错了什么以及我该如何解决。我仍然是初学者,所以如果您有任何关于修复错误或编写代码的建议,我会非常乐意。

main.cpp :

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

int main ()
{
    Game Game;

    Game.GameRun();
}

游戏.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;
};

游戏.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>main.obj:错误 LNK2001:未解析的外部符号“public:static int Character::characterY”(?characterY@Character@@2HA)

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

1>C:\Users\SONY\Documents\Visual Studio 2010\Projects\Top Down Game\Debug\Game.exe : fatal error LNK1120: 2 unresolved externals

谢谢:D

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

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

最佳答案

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

背后的逻辑不起作用,所以我对其进行了最低限度的修复,以便在修复编译(链接)错误后立即运行。您应该尝试我的代码,看看它是否有开箱即用的帮助。

先解释一下

按值传递播放器对象(当您有 void someMethod(Character player) 时)会将对象的拷贝发送到方法。因此,当您对播放器对象进行更改时,这些更改只会对拷贝进行,而不会对实际播放器进行更改。我更改了您的代码,因此它改为发送一个指针 ( void someMethod(Character * player) 。该方法接收指向实际播放器对象的指针,对该对象所做的更改反射(reflect)在实际播放器对象上。

除了使用指针(有些人可能会说这太过分或太危险)之外,还有几个解决方案。

它可以代替:

  • 引用 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();
}

游戏.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

游戏.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++ 信息

关于c++ - 使用 C++ SFML Unresolved LNK 2001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236632/

相关文章:

c++ - 关于模板声明中声明的措辞不明确

c++ - 如何捕获 WndProc() 函数中定义的静态对象的异常?

c++ - 有什么方法可以从 Visual Studio 生成 nmake 吗?

c++ - Bullet Physics 刚体从表面反弹

c++ - Boost.Spirit X3 中的错误处理和注释

c++ - 我的 sfml 纹理在哪里超出范围?

c++ - 无法在 Linux 中运行使用 sfml 的程序

c++ - 仅接受来自特定 IP 的套接字

c++ - -1073741811(0xc000000d)错误opencv

visual-studio-2010 - $exception 伪变量在 Visual Studio 调试器中不可用(美元异常)