错误LNK2005:已在Game.obj中定义“class Player m_player”

时间:2014-12-08 11:49:45

标签: c++ debugging

我正在阅读SDL Game Development本书,但我花了太多时间来调试这些示例而非专注于这些内容。现在我遇到了这个问题,我找不到解决方案

error LNK2005: "class Player m_player" (?m_player@@3VPlayer@@A) already defined in Game.obj
error LNK2005: "class GameObject m_go" (?m_go@@3VGameObject@@A) already defined in Game.obj

我已发布相关课程

Game.cpp
Game.h
GameObject.cpp
GameObject.h
Player.cpp
Player.h

game.cpp

#include <iostream>
#include "Game.h"


Game::Game(void)
{
}


Game::~Game(void)
{
}


bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags)
{
    if ( SDL_Init(SDL_INIT_EVERYTHING) == 0 ){
        std::cout << "SDL init success\n";
        m_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",xpos,ypos,640, 480, flags);

        //_________________________________________________________
        if ( m_pWindow != 0 ){
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
            //_______________________________________________________
            if ( m_pRenderer != 0 ){
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255);
            }else{
                std::cout << "renderer init fail\n";
                return false;
            }
            //_______________________________________________________
        }else{
            std::cout << "window init fail\n";
            return false;
        }
        //___________________________________________________________
    }else{
        std::cout << "SDL init fail\n";
        return false;
    }

    std::cout << "init success\n";
    m_bRunning = true;

    ////////////////////////////////////////////////////////////////////

    /*if ( TheTextureManager::Instance()->load("animate-alpha.png", "animate", m_pRenderer) )
        return false;*/

    m_go.load(100, 100, 128, 82, "animate");
    m_player.load(300, 300, 128, 82, "animate");

    return true;
}


void Game::render()
{
    SDL_RenderClear(m_pRenderer);
        //TheTextureManager::Instance()->draw("animate", 0,0, 128, 82, m_pRenderer);
        //TheTextureManager::Instance()->drawFrame("animate", 100, 100, 128, 82, 1, m_currentFrame, m_pRenderer);
        m_go.draw(m_pRenderer);
        m_player.draw(m_pRenderer);
    SDL_RenderPresent(m_pRenderer);
}

void Game::clean()
{
    std::cout << "cleaning game\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

void Game::handleEvents()
{
    SDL_Event Event;
    if ( SDL_PollEvent(&Event) ){
        switch( Event.type )
        {
            case SDL_QUIT:
                m_bRunning = false;
                break;

            default:
                break;
        }
    }
}


void Game::update()
{
    //m_currentFrame =  int(((SDL_GetTicks() / 100) % 6));
    m_go.update();
    m_player.update();
}

game.h

#pragma once
#include <SDL.h>
//#include "TextureManager.h"
//#include "GameObject.h"
#include "Player.h"

GameObject m_go;
Player m_player;


class Game
{
public:
    Game(void);
    ~Game(void);

    bool init(const char*, int, int, int, int, int); 

    void render();
    void update();
    void handleEvents();
    void clean();

    inline bool running() { return m_bRunning; }

private:

    SDL_Window*   m_pWindow;
    SDL_Renderer* m_pRenderer;

    int m_currentFrame;
    bool m_bRunning;


};

GameObject.h

    #pragma once
    #include <iostream>
    #include <string>
    #include <SDL.h>
    #include "TextureManager.h"

    class GameObject
    {
    public:
        GameObject(void);
        ~GameObject(void);

        void load(int x, int y, int width, int height, std::string textureID);

        void draw(SDL_Renderer* pRenderer); 
        void update();
        void clean();

    protected:
        std::string m_textureID;

        int m_currentFrame;
        int m_currentRow;

        int m_x, m_y;
        int m_width, m_height;
    };

Player.cpp

#include "Player.h"


Player::Player(void)
{
}


Player::~Player(void)
{
}

void Player::load(int x, int y, int width, int height, std::string textureID)
{
    GameObject::load(x, y, width, height, textureID);
}


void Player::draw(SDL_Renderer* pRenderer)
{
    GameObject::draw(pRenderer);
}

void Player::update()
{
    m_x -= 1;
}

void Player::clean()
{

}

Player.h

#pragma once
#include "GameObject.h"

class Player : public GameObject
{
public:
    Player(void);
    ~Player(void);

    void load(int x, int y, int width, int height, std::string textureID);
    void draw(SDL_Renderer* pRenderer);
    void update();
    void clean();
};

1 个答案:

答案 0 :(得分:2)

如错误所述,您无法多次定义GameObject m_go;Player m_player(标头文件冲突)。

要解决此问题,请移除#include的{​​{1}}或者创建新标题并移动

game.h

到该文件,然后GameObject m_go; Player m_player;您需要这两个变量的文件。

正如Piotr建议的那样,这本书的作者可能意味着您需要将这两行放在#include类声明中Game下。