此错误是什么意思以及如何解决它:错误LNK2005:"<symbol>"已在 <file.obj 中定义>



目前,我正在Visual Studio 2017中使用SFML,并在第2部分中使用本教程。到目前为止,我已经取得了成功,但我试图启动视频的最终产品,并得到了这些错误:

1>Main.cpp
1>Main.obj : error LNK2005: "public: __thiscall Game::Game(void)" (??0Game@@QAE@XZ) already defined in Game.obj
1>Main.obj : error LNK2005: "public: virtual __thiscall Game::~Game(void)" (??1Game@@UAE@XZ) already defined in Game.obj
1>Main.obj : error LNK2005: "private: void __thiscall Game::initVariables(void)" (?initVariables@Game@@AAEXXZ) already defined in Game.obj
1>Main.obj : error LNK2005: "private: void __thiscall Game::initWindow(void)" (?initWindow@Game@@AAEXXZ) already defined in Game.obj
1>Main.obj : error LNK2005: "public: void __thiscall Game::pollEvents(void)" (?pollEvents@Game@@QAEXXZ) already defined in Game.obj
1>Main.obj : error LNK2005: "public: void __thiscall Game::render(void)" (?render@Game@@QAEXXZ) already defined in Game.obj
1>Main.obj : error LNK2005: "public: bool const __thiscall Game::running(void)const " (?running@Game@@QBE?B_NXZ) already defined in Game.obj
1>Main.obj : error LNK2005: "public: void __thiscall Game::update(void)" (?update@Game@@QAEXXZ) already defined in Game.obj

根据我的理解,它说我试图描述main.cpp中已经在game.cpp中定义的所有内容(或.obj,我不知道那是什么,我认为它只是代码的编译版本)。

下面是我的代码:

main.cpp

#include <iostream>
#include "Game.cpp"
int main()
{
//Init game
Game game;
while (game.running()) //Checks if window is open
{
game.update();
game.render();
} 
}

Game.cpp

#include "Game.h"
#include <SFML/Graphics.hpp> 
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
//if there isn't a comment then don't modify it
void Game::initVariables()
{
this->window = nullptr;
}
void Game::initWindow()
{
this->vidMode.height = 480;
this->vidMode.width = 640;
this->window = new sf::RenderWindow(this->vidMode, "gaymer time", sf::Style::Close | sf::Style::Titlebar | sf::Style::Resize);
}
Game::Game()
{
this->initVariables();
this->initWindow();
}
Game::~Game()
{
delete this->window;
}
const bool Game::running() const
{
return this->window->isOpen();
}
void Game::pollEvents()
{
while (this->window->pollEvent(this->event)) //Does sumthin idk
{
switch (this->event.type)
{
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
this->window->close();
break;
}
}
}
//Functions
void Game::update()//Updates game (does the actual gamer stuff)
{
this->pollEvents();
}
void Game::render()//Renders game (just puts the pixels on the screen)
{
this->window->clear(sf::Color::Transparent); //Clear old frame
//Draw new frame
this->window->display(); //Displays new frame
}

Game.h

#pragma once
#include <SFML/Graphics.hpp> 
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
class Game
{
private:
//Private Variables
sf::RenderWindow* window;
sf::VideoMode vidMode;
sf::Event event;
//Private Functions
void initVariables();
void initWindow();
public:
//Constructors/Destructors
Game();
virtual ~Game();
//Accessors
const bool running() const;
//Functions
void pollEvents();
void update();
void render();
};

如果你需要更多的信息,尽管问。

不要在其他源文件中包含源文件。

在这种情况下,game.cpp中的代码将被编译两次,一次编译成main.obj,一次编译成game.obj。然后链接器将合并obj文件并找到重复的定义,从而找到错误。

main.cpp中,使用#include "Game.h"代替#include "Game.cpp"

最新更新