【问题标题】:LNK 2001 unresolved with C++ SFMLLNK 2001 未使用 C++ SFML 解决
【发布时间】:2014-04-09 19:20:31
【问题描述】:

我正在尝试使用 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>main.obj : error LNK2001: unresolved external symbol "public: static int Character::characterY" (?characterY@Character@@2HA)

1>main.obj : error LNK2001: unresolved external symbol "public: static int Character::characterX" (?characterX@Character@@2HA)

1>C:\Users\SONY\Documents\Visual Studio 2010\Projects\Top Down Game\Debug\Game.exe : 致命错误 LNK1120: 2 unresolved externals

谢谢:D

编辑:仅供参考,我的代码没有类,并且运行良好,它只是 main.cpp 中的一大块代码

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

【问题讨论】:

  • 给出的错误是什么? LNK 2001 表示未解析的符号,它也应该为您提供该符号的名称。
  • 您在main.cpp 中#include Game.cpp。不要那样做!
  • 您的 Character 类与这些错误不匹配。错误说它们被声明为static,但您的代码却不然。
  • @faranwath 我不需要它来运行该功能?
  • @SamCristall 我知道这真的很奇怪!!!我有一种感觉视觉工作室把我搞砸了......

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


【解决方案1】:

EDIT 2014/03/08:我试过你的消息来源。删除Main.cpp 文件中的#include "Game.cpp"Game.cpp 文件中的inline 关键字。

背后的逻辑不起作用,因此我将其修复为最低限度,以便在修复编译(链接)错误后立即起作用。你应该试试我的代码,看看它是否有帮助。

先解释一下

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

除了使用指针(有些人可能会说这是矫枉过正或太危险)之外,还有一些解决方案。

它可能是:

  • 参考void someMethod(Character&amp; player)
  • 或返回值Character someMethod(Character player)
  • 或两者的混合Character someMethod(const Character&amp; 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++ 信息

【讨论】:

  • 嘿,我得到了同样的错误,实际上我得到了一个额外的错误:错误 LNK2005: "public: void __thiscall Game::GameRun(void)" (?GameRun@Game@@QAEXXZ) 已经定义在 Game.obj 中,您能否以更简单的方式解释更多您所做的事情?毕竟我是初学者:D
  • 我确实点击了全部显示并意识到精灵表没有“包含在项目中”。错误仍然存​​在...由于某种原因,将 game.cpp 中的函数更改为内联会导致“已定义”错误消失。
  • 我复制粘贴的你的代码来替换我的。如果我删除内联,我会得到“已经定义的错误”
  • 我刚刚删除了 Game.cpp 的#include,其中一个未解决的外部文件消失了!!!还剩一个:错误LNK2019:函数_main中引用的未解析外部符号“public:void __thiscall Game::GameRun(void)”(?GameRun@Game@@QAEXXZ)
  • 您能否打包/压缩 MS Visual Studio 项目并将其上传到某个地方(如保管箱)并共享它,这样我就可以真正看到发生了什么。因为现在,问题似乎不是代码,我的意思是从我的角度来看它工作正常......
猜你喜欢
  • 2012-09-04
  • 1970-01-01
  • 2019-02-26
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
相关资源
最近更新 更多