cuốn sách gpt4 ai đã làm

c++ - gcc - 定义模板时出现多重定义错误(vc++ 正常)

In lại Tác giả: Walker 123 更新时间:2023-11-28 02:41:05 32 4
mua khóa gpt4 Nike

tôi đang sử dụng EnumParsertừ đây它在 VC++ 中编译得很好,但是使用 gcc 我有这样的错误:

./Terminator.o: In function `EnumParser::EnumParser()':
Terminator.cpp:(.text+0x960): multiple definition of `EnumParser::EnumParser()'
./MicexGate.o:MicexGate.cpp:(.text+0xd0): first defined here
./Terminator.o: In function `EnumParser::EnumParser()':
Terminator.cpp:(.text+0x960): multiple definition of `EnumParser::EnumParser()'
./MicexGate.o:MicexGate.cpp:(.text+0xd0): first defined here
./Terminator.o: In function `EnumParser::EnumParser()':

好像EnumParser::EnumParser()出现在两个MicexGate.oTerminator.o这就是问题所在。但我不知道为什么这是一个错误以及如何解决它。

在我的程序中,我只在 .cpp 中定义了一次 EnumParser文件在 MicexGate静态库项目。 Terminator取决于 MicexGate可能这就是为什么最终 EnumParser 定义了两次。这就是我定义 EnumParser 的方式:

#include "FieldsConverter.h"
#include
#include
#include
#include "ByteArrayReader.h"
#include "Utils.h"
#include "CommonsMicexBridge.h"
#include "InstrumentsStorage.h"
#include

template<> EnumParser::EnumParser()
{
enumMap["Char"] = Char;
enumMap["Integer"] = Integer;
enumMap["Long"] = Long;
enumMap["Fixed"] = Fixed;
enumMap["Price"] = Price;
enumMap["Date"] = Date;
enumMap["Time"] = Time;
}

我该如何解决我的问题?

câu trả lời hay nhất

我的猜测是您没有在 header 中声明显式特化,包含在每个使用特化的文件中:

template<> EnumParser::EnumParser();

如果没有这个声明,编译器就不知道显式特化的存在,因此如果需要的话,将从通用模板中实例化一个隐式特化。您现在有两个定义,(希望如此)导致链接错误。

或者,与任何函数一样,您可以在 header 中定义它,只要您声明它nội tuyến 以允许在多个翻译单元中定义。

关于c++ - gcc - 定义模板时出现多重定义错误(vc++ 正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973806/

32 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress