成员函数已在 .obj 中定义 (visual studio)

Member function already defined in .obj (visual studio)

我从来没有在代码块中遇到过这个问题,但是在 visual studio 类 中给我错误 LNK2005 ... 已经在 xx.obj 中定义了。我读过很多答案,其中大部分都说要使用 'extern',我认为它不能用于成员函数

Error: 1>Source.obj : error LNK2005: "public: __thiscall Game::Game(void)" (??0Game@@QAE@XZ) already defined in game.obj

Error: fatal error LNK1169: one or more multiply defined symbols found

如果没有粗暴的 forcing/allowing 多个定义,我该如何解决这个问题?

//game.h

#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics.hpp>

class Game
{
    public:
        Game();

    private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;
};

#endif

.

//game.cpp

#include "..\Headers\game.h"

Game::Game()
    : mWindow(sf::VideoMode(640, 480), "Beginning")
    , mPlayer()
{
    mPlayer.setRadius(40.f);
    mPlayer.setPosition(100.f, 100.f);
    mPlayer.setFillColor(sf::Color::Cyan);
}

.

//source.cpp

#include "game.cpp"

int main()
{
    Game game;
}

替换成你的source.cpp

#include "game.cpp"

来自

#include "game.h"

您永远不应包含 *.cpp。