C++项目中出现错误LNK2005

Error LNK2005 in C++ project

本文关键字:错误 LNK2005 项目 C++      更新时间:2023-10-16

由于错误LNK2005 ,我的功能出现问题

void createNetwork(Link **linkTable, Router **router, Computer **computer) {
router = new Router*[5];
linkTable = new Link*[11];
computer = new Computer*[6];
//---------------------------------------------------------------
router[0] = NULL;
//---------------------------------------------------------------
router[1] = new Router(1);
router[1]->link = new Link*[4];
   ....

错误:

1>device.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router      * *,class Computer * *)" (? createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>Link.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>Packet.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>Queue.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>Router.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>sc.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>Time.obj : error LNK2005: "void __cdecl createNetwork(class Link * *,class Router * *,class Computer * *)" (?createNetwork@@YAXPAPAVLink@@PAPAVRouter@@PAPAVComputer@@@Z) already defined in Computer.obj
1>E:DokumentySymulacja - Projekttrunkf4_sk_zelek_witDebugf4_sk_zw.exe : fatal error LNK1169: one or more multiply defined symbols found

我试着把它修好,但没用。有人能告诉我该怎么修吗?Thx:)

如果要在标头中定义函数,请将其标记为inline,以允许在包含标头的每个翻译单元中进行定义。

或者,将定义移动到源文件中,使其只定义一次。