Duplicate symbol error in C++ and Xcode -
i'm trying declare class acts enum. if include more once, several "duplicated symbol" errors.
this itemtype.h
file
#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) { (int = 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
and 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};
in first attempt tried put c++ code header file, , got same errors. don't know doing wrong.
can me, please?
many thanks!
you can't define non-inline
functions outside class in header file.
to solve this, have 3 possibilities:
- move definitions of
operator==
,operator!=
inside class definition. - move definitions
itemtype.cpp
. - declare functions
inline
.
Comments
Post a Comment