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

Member function already defined in .obj (visual studio)

本文关键字:studio 定义 visual 函数 obj 成员      更新时间:2023-10-16

我在代码块中从未遇到过这个问题,但在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

如何在不使用暴力/允许多个定义的情况下解决此问题?

//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 "..Headersgame.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;
}

在源中替换.cpp

#include "game.cpp"

通过

#include "game.h"

您永远不应该包含*.cpp.