0

我正在尝试使用 c++ 和 SMFL 2.0 创建游戏,但我无法通过此错误,这让我发疯。我一直在互联网上阅读,似乎它可能是由于忘记包含而引起的,但我仍然看不出我哪里出错了。我已将 SMFL 2.0 库包含在 c++ -> 常规 -> 附加包含目录和链接器 -> 常规 -> 附加库目录下的项目属性部分中。

我完全空白,这是我收到的错误消息:

1>------ Build started: Project: Pang, Configuration: Debug Win32 ------
1>  Game.cpp
1>  Generating Code...
1>  Skipping... (no relevant changes detected)
1>  Pang.cpp
1>Game.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>Game.obj : error LNK2001: unresolved external symbol "private: static class PlayerPaddle Game::_player1" (?_player1@Game@@0VPlayerPaddle@@A)
1>C:\Users\Alexander\Desktop\C++\Projects\Pang\Debug\Pang.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

当我环顾代码时,Microsoft Visual C++ 没有显示任何带有红色下划线的语法错误,因此看起来语法没问题。以下是相关的代码行:

我的大部分一般都包括在这里:

标准数据文件

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>

#include <map>
#include <iostream>
#include <cassert>
#include <list>

可见游戏对象.h

#pragma once

class VisibleGameObject {

    public:
        VisibleGameObject();
        virtual ~VisibleGameObject();

        virtual void Load(std::string filename);
        virtual void Draw(sf::RenderWindow &window);

        virtual void SetPosition(float x, float y);

    private:
        sf::Sprite  _sprite;
        sf::Texture _image;
        std::string _filename;
        bool _isLoaded;

};

可见游戏对象.cpp

#include "stdafx.h"
#include "VisibleGameObject.h"

VisibleGameObject::VisibleGameObject() {
    _isLoaded = false;
}
VisibleGameObject::~VisibleGameObject() {
}
void VisibleGameObject::Load(std::string filename) {

    if (_image.loadFromFile(filename) == false) {
        _filename = "";
        _isLoaded = false;
    }
    else {
        _filename = filename;
        _sprite.setTexture(_image);
    }
}

void VisibleGameObject::Draw(sf::RenderWindow &renderWindow) {

    if (_isLoaded) {
        renderWindow.draw(_sprite);
    }
}

void VisibleGameObject::SetPosition(float x, float y) {

    if (_isLoaded) {
        _sprite.setPosition(x,y);
    }

}

这个类帮助我使用 SMFL 库在渲染窗口上绘制图像。

PlayerPaddle.h

#pragma once
#include "VisibleGameObject.h"

class PlayerPaddle : public VisibleGameObject {

    public:
        PlayerPaddle();
        ~PlayerPaddle();
};

PlayerPaddle.cpp

#include "stdafx.h"
#include "VisibleGameObject.h"
#include "PlayerPaddle.h"

PlayerPaddle::PlayerPaddle() {
}
PlayerPaddle::~PlayerPaddle() {
}

游戏.h

#pragma once
#include "PlayerPaddle.h"

class Game {

public:
    static void Start();

private:
    static bool IsExiting();
    static void GameLoop();

    static void ShowSplashScreen();
    static void ShowMenu();

    enum GameState { Uninitialized, ShowingSplash, Paused, ShowingMenu, Playing, Exiting };

    static GameState _gameState;
    static sf::RenderWindow _mainWindow;
    static PlayerPaddle _player1;
};

游戏.cpp

#include "stdafx.h"
#include "Game.h"
#include "SplashScreen.h"
#include "MainMenu.h"


void Game::Start(void) {

    if(_gameState != Uninitialized) {
        return;
    }

    _mainWindow.create(sf::VideoMode(1024,768,32),"Pang!");

    //_player1.Load("images/paddle.png");
    //_player1.SetPosition((1024/2)-45, 700);

    _gameState = Game::ShowingSplash;

    while(!IsExiting()) {
        GameLoop();
    }

    _mainWindow.close();
}

bool Game::IsExiting() {

    if(_gameState == Game::Exiting) {
        return true;
    }
    else {
        return false;
    }
}

void Game::GameLoop() {

    switch(_gameState) {

        case Game::ShowingMenu: {
            ShowMenu();
            break;
        }
        case Game::ShowingSplash: {
            ShowSplashScreen();
            break;
        }
        case Game::Playing: {
            sf::Event currentEvent;
            while (_mainWindow.pollEvent(currentEvent)) {
                _mainWindow.clear(sf::Color(0,0,0));
                //_player1.Draw(_mainWindow);
                _mainWindow.display();

                if (currentEvent.type == sf::Event::Closed) {
                    _gameState = Game::Exiting;
                }

                if (currentEvent.type == sf::Event::KeyPressed) {
                    return;

                    if(currentEvent.key.code == sf::Keyboard::Escape) {
                        ShowMenu();
                    }

                }
            }
            break;
        }
    }
}

void Game::ShowSplashScreen() {
    SplashScreen splashScreen;
    splashScreen.Show(_mainWindow);
    _gameState = Game::ShowingMenu;
}

void Game::ShowMenu() {
    MainMenu mainMenu;
    MainMenu::MenuResult result = mainMenu.Show(_mainWindow);
    switch(result) {
        case MainMenu::Exit: {
            _gameState = Game::Exiting;
            break;
        }
        case MainMenu::Play: {
            _gameState = Game::Playing;
            break;
        }
    }
}

Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;

此代码仅在 Game.cpp 中以 _player1 开头的 3 行代码被注释掉时编译和运行。当没有被注释掉时,我得到了这个 LNK2001 错误,我就是无法通过这个,这非常令人沮丧。

如果有人对这里可能出现的问题提出意见,那就太好了。

最好的问候,亚历山大

4

3 回答 3

4

您的 game.cpp 缺少 Game::_player1 的定义:

PlayerPaddle Game::_player1;
于 2013-05-02T16:30:50.637 回答
0

1)尝试公开继承PlayerPaddle类到Game类。它有效。例子

类游戏:public PlayerPaddle

2)或者您可以使用组合。在游戏中使用指向 PlayerPaddle 的指针或静态指针。利用

静态PlayerPaddle* _player1;

它应该工作。

于 2013-05-02T16:50:13.500 回答
0

您正确定义了静态成员变量_gameState_mainWindow,但不是_player1

于 2013-05-02T16:31:31.633 回答