我试图增加一个枚举类型,所以我以这种方式重载了运算符++:
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)
我该如何解决?什么是增加枚举类型的正确方法或更好的方法?
最佳答案
看来您的operator ++已从多个编译单元中导出。这可能导致重复的符号错误,其原因可能是头文件中operator ++的定义(不仅仅是声明)。以下选项可用于解决问题:
inline
回答第二个问题(关于增加枚举类型的更好方法),我认为这是C++ 11中的以下内容(clang编译器的-std = c++ 11选项):
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;
}
关于c++ - 架构x86_64的C++重复符号..递增枚举类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31132747/