我使用EnumParser
从here它编译在VC精细++,但用gcc我有这样的错误:GCC - 多重定义错误定义模板时(VC++罚款)
./Terminator.o: In function `EnumParser<FieldType>::EnumParser()':
Terminator.cpp:(.text+0x960): multiple definition of `EnumParser<FieldType>::EnumParser()'
./MicexGate.o:MicexGate.cpp:(.text+0xd0): first defined here
./Terminator.o: In function `EnumParser<FieldType>::EnumParser()':
Terminator.cpp:(.text+0x960): multiple definition of `EnumParser<FieldType>::EnumParser()'
./MicexGate.o:MicexGate.cpp:(.text+0xd0): first defined here
./Terminator.o: In function `EnumParser<FieldsetName>::EnumParser()':
看来EnumParser<FieldType>::EnumParser()
中均出现MicexGate.o
和Terminator.o
,这是问题所在。但我不知道为什么这是一个错误,以及如何解决它。
在我的程序中,我只在.cpp
文件的MicexGate
static lib项目中定义了这个EnumParser。 Terminator
取决于MicexGate
可能这就是为什么最后EnumParser定义了两次。这是我如何定义EnumParser<FieldType>
:
#include "FieldsConverter.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
#include "ByteArrayReader.h"
#include "Utils.h"
#include "CommonsMicexBridge.h"
#include "InstrumentsStorage.h"
#include <boost/algorithm/string.hpp>
template<> EnumParser<FieldType>::EnumParser()
{
enumMap["Char"] = Char;
enumMap["Integer"] = Integer;
enumMap["Long"] = Long;
enumMap["Fixed"] = Fixed;
enumMap["Price"] = Price;
enumMap["Date"] = Date;
enumMap["Time"] = Time;
}
我怎样才能解决我的问题?
你是否声明了明确的特化,并在每个使用它的文件中包含该声明?如果编译器不知道它,那么它将专门化通用模板而不是专业化,导致多个定义。 – 2014-09-22 12:26:48