我试图增加枚举类型,所以我以这种方式重载了运算符:
enum QuarterType{
FIRST_QUARTER,
SECOND_QUARTER,
THIRD_QUARTER,
FOURTH_QUARTER
};
重载Operator ++:
QuarterType& operator++(QuarterType& quarter){
switch (quarter) {
case FIRST_QUARTER:
return quarter= SECOND_QUARTER;
case SECOND_QUARTER:
return quarter=THIRD_QUARTER;
case THIRD_QUARTER:
return quarter=FOURTH_QUARTER;
case FOURTH_QUARTER:
return quarter=FIRST_QUARTER;
}
}
但是当我编译项目时,我收到错误:
duplicate symbol __ZN3mtmppERNS_11QuarterTypeE in:
/Users/../Library/Developer/Xcode/DerivedData/mtm-crtygivbbxwmodgeasndvnjnpczt/Build/Intermediates/mtm.build/Debug/mtm.build/Objects-normal/x86_64/SecurityExample.o
/Users/../Library/Developer/Xcode/DerivedData/mtm-crtygivbbxwmodgeasndvnjnpczt/Build/Intermediates/mtm.build/Debug/mtm.build/Objects-normal/x86_64/Quarters.o
duplicate symbol __ZN3mtmppERNS_11QuarterTypeE in:
/Users/../Library/Developer/Xcode/DerivedData/mtm-crtygivbbxwmodgeasndvnjnpczt/Build/Intermediates/mtm.build/Debug/mtm.build/Objects-normal/x86_64/SecurityExample.o
/Users/../Library/Developer/Xcode/DerivedData/mtm-crtygivbbxwmodgeasndvnjnpczt/Build/Intermediates/mtm.build/Debug/mtm.build/Objects-normal/x86_64/Security.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我该如何解决?什么是正确的方法或增加枚举类型的更好方法?
答案 0 :(得分:3)
似乎您的operator ++从多个编译单元导出。这可能导致重复的符号错误,其原因可能是头文件中operator ++的定义(不仅仅是声明)。可以使用以下选项来解决问题:
inline
回答第二个问题(关于增加枚举类型的更好方法),我认为它是C ++ 11中的以下内容(-std = c ++ 11选项到clang编译器):
enum class QuarterType : int {
FIRST_QUARTER = 0,
SECOND_QUARTER = 1,
THIRD_QUARTER = 2,
FOURTH_QUARTER = 3,
TOTAL_QUARTERS = 4
};
inline QuarterType& operator++(QuarterType& quarter) {
int currentQuarter = static_cast<int>(quarter);
int nextQuarter = (currentQuarter+1)
% static_cast<int>(QuarterType::TOTAL_QUARTERS);
quarter = static_cast<QuarterType>(nextQuarter);
return quarter;
}