C++和 Xcode 中出现重复符号错误
Duplicate symbol error in C++ and Xcode
>我正在尝试声明一个充当枚举的类。但是,如果我多次包含它,则会出现几个"重复符号"错误。
这是我ItemType.h
文件
#ifndef DarkSnake_ItemType_h
#define DarkSnake_ItemType_h
#define COLOR_ITEM_1 0xffff00ff
#define COLOR_ITEM_2 0xffff03ff
#define COLOR_ITEM_3 0xffff06ff
class ItemType {
public:
static const ItemType NONE;
static const ItemType ITEM_1;
static const ItemType ITEM_2;
static const ItemType ITEM_3;
static ItemType values[];
static ItemType getItemTypeByColor(const int color) {
for (int i = 0; 3; i++) {
if (color == values[i].getItemColor()) {
return values[i];
}
}
return NONE;
}
bool operator ==(const ItemType &item) const;
bool operator !=(const ItemType &item) const;
int getItemColor() { return this->color; };
private:
const int color;
ItemType(const int _color) : color(_color) {}
};
bool ItemType::operator == (const ItemType &item) const {
return this->color == item.color;
}
bool ItemType::operator != (const ItemType &item) const {
return this->color != item.color;
}
#endif
这是我ItemType.cpp
:
#include "ItemType.h"
const ItemType ItemType::NONE = ItemType(0);
const ItemType ItemType::ITEM_1 = ItemType(COLOR_ITEM_1);
const ItemType ItemType::ITEM_2 = ItemType(COLOR_ITEM_2);
const ItemType ItemType::ITEM_3 = ItemType(COLOR_ITEM_3);
ItemType ItemType::values[] = {ItemType::ITEM_1, ItemType::ITEM_2, ItemType::ITEM_3};
在第一次尝试中,我尝试将C++代码放入头文件中,但遇到了相同的错误。但现在我不知道我做错了什么。
你能帮帮我吗?
非常感谢!
不能
在头文件中定义类外部的非inline
函数。
要解决此问题,您有三种可能性:
- 在类定义中移动
operator==
和operator!=
的定义。 - 将定义移动到
ItemType.cpp
。 - 声明函数
inline
。
相关文章:
- 在C++中使用内联方法时出现未定义的符号错误
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 函数指针给我"重新声明为不同类型的符号"错误
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 函数路径追加上未解决的外部符号错误
- 作为成员的静态类模板实例会导致未解析的外部符号错误
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 共享库中使用"使用命名空间"定义的符号错误
- 在FreesWitch中使用AWS C SDK加载模块时未定义的符号错误
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- C++编译错误LNK2019:未解决的外部符号错误
- VTK / ITK/ QT - 未解决的外部符号错误 (LNK2001)
- QT创建者:简单程序未解决的外部符号和外部符号错误
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 由于使用未签名的INT,因此在Variadic模板C 上未解决的外部符号错误
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- 使用从WINRT网格类派生的抽象类时,Casttobase未解决的外部符号错误
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See