C++ C+中的重复符号错误+;和Xcode

C++ C+中的重复符号错误+;和Xcode,c++,xcode,cocos2d-x,C++,Xcode,Cocos2d X,我试图声明一个充当枚举的类。但如果我不止一次地包含它,就会出现几个“复制符号”错误 这是我的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

我试图声明一个充当枚举的类。但如果我不止一次地包含它,就会出现几个“复制符号”错误

这是我的
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++代码放入头文件中,并且得到相同的错误。但现在我不知道我做错了什么

你能帮帮我吗


非常感谢

不能在头文件中的类之外定义非
内联
函数

要解决此问题,您有三种可能:

  • 移动
    运算符==
    运算符的定义=在类定义中
  • 将定义移动到
    ItemType.cpp
  • 声明函数
    内联

  • 伟大的我已经使用了第一种方法,它很有效^非常感谢!