首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内联文件中的C++ LNK2005

内联文件中的C++ LNK2005
EN

Stack Overflow用户
提问于 2015-05-20 17:43:59
回答 1查看 396关注 0票数 0

如标题所示,我的内联文件有一个链接器错误。它抱怨我的dungeon_layout头(包括inl文件)定义了两次dungeon_room头。我所有的头都有保护,我只包含.hpp文件,但是当我试图重载赋值操作符时,它仍然很生气。它所投诉的守则如下:

我需要分配任务1:

代码语言:javascript
复制
 dungeon_room dungeon_layout::getCurrentRoom()
    {
        ...

        dungeon_room temp;

        for (row = levelLayout.begin(); row != levelLayout.end(); row++)
            {
                for (col = row->begin(); col != row->end(); col++)
                    {
                        if (col->active)
                            {
                                ...
                                temp = *col;
                                ...
                            }
                    }
                ...
            }

        return temp;
    }

我需要任务2:

代码语言:javascript
复制
dungeon_room::dungeon_room()
{
    ...
    roomMap = tim::tileMap(20, 20, 50);
    ...
}

赋值操作员1:

代码语言:javascript
复制
tileMap tim::tileMap::operator=(const tileMap& other)
{
    // nothing...
}

赋值操作员2:

代码语言:javascript
复制
void dungeon_room::operator=(const dungeon_room& other)
{
    // nothing...
}

Tile Manager的头(函数的inl文件定义)

代码语言:javascript
复制
#ifndef DUNGEON_ROOM_HPP
#define DUNGEON_ROOM_HPP

...
#include "gridManager\tileManager.hpp"
...
#endif

级别布局的标头

代码语言:javascript
复制
#ifndef DUNGEON_LAYOUT_HPP
#define DUNGEON_LAYOUT_HPP

...

#include "dungeon_room.hpp"

...
#endif

错误:

代码语言:javascript
复制
1>dungeon_room.obj : error LNK2005: "private: void __cdecl 

tim::tileMap::addSprite(class sf::RectangleShape,unsigned __int64)" (?addSprite@tileMap@tim@@AEAAXVRectangleShape@sf@@_K@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "private: void __cdecl tim::tileMap::addSprite(class sf::Sprite,unsigned __int64)" (?addSprite@tileMap@tim@@AEAAXVSprite@sf@@_K@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::modifyTile(int,int,int)" (?modifyTile@tileMap@tim@@QEAAXHHH@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::addCollisionValue(int)" (?addCollisionValue@tileMap@tim@@QEAAXH@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow &,class sf::View &)" (?drawTiles@tileMap@tim@@QEAAXAEAVRenderWindow@sf@@AEAVView@4@@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow *,class sf::View &)" (?drawTiles@tileMap@tim@@QEAAXPEAVRenderWindow@sf@@AEAVView@4@@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow &)" (?drawTiles@tileMap@tim@@QEAAXAEAVRenderWindow@sf@@@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow *)" (?drawTiles@tileMap@tim@@QEAAXPEAVRenderWindow@sf@@@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::operator=(class tim::tileMap const &)" (??4tileMap@tim@@QEAAXAEBV01@@Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: __cdecl tim::tileMap::tileMap(void)" (??0tileMap@tim@@QEAA@XZ) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: __cdecl tim::tileMap::tileMap(float,float,float)" (??0tileMap@tim@@QEAA@MMM@Z) already defined in dungeon_layout.obj
1>F:\Desktop Files\C++\Games\Dungeon_Seeker\x64\Debug\Dungeon_Seeker.exe : fatal error LNK1169: one or more multiply defined symbols found

我相信这应该是有关的守则。我已经被困住了,调试已经有一段时间了,而且一直很烦人。帮帮忙,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-20 22:37:10

您之所以会出现这些错误,是因为正在为其编译的每个.cpp文件编译函数,然后对包含内联文件的每个.cpp文件进行多次导出。

包含定义的.inl文件被粘贴到包含定义的每个.cpp文件中。这意味着,当程序链接在一起时,就会有一个函数的定义,这个定义应该只在几个文件中定义一次。这违反了一个定义规则,也是链接器给出错误的原因。

要解决这个问题,你有三个选择。首先,您可以将函数标记为inline,这不一定使编译器内联该函数,而是允许多次定义函数。其次,可以将函数的定义移到类的定义中,这将函数隐式标记为内联。也可以在.cpp文件中定义函数。

使用代码,如果仍然希望在.inl文件中声明函数,可以将inline放在错误消息中函数定义的前面。在这种情况下,这些函数是

代码语言:javascript
复制
tim::tileMap::addSprite
tim::tileMap::modifyTile
tim::tileMap::addCollisionValue
tim::tileMap::drawTiles         //All 4 overloads
tim::tileMap::operator=
tim::tileMap::tileMap           //Both constructor overloads
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30356800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档