I have a class EventType
, which has the following header (relevant lines only):
#include<string>
#include<unordered_set>
#include<iostream>
class EventType
{
public:
static EventType* getByName(std::string name);
static EventType* getByID(std::string id);
static void setAllEventTypes(std::unordered_set<EventType*> events);
//...
private:
static std::unordered_set<EventType*> allEvents; //stores all events
std::string name;
//...
std::string akaID;
};
And the source:
EventType* EventType::getByName(std::string name) {
foreach(EventType * event, EventType::allEvents) {
if(event->name == name) {
return event;
}
}
std::cout << "Error: Event with name " << name << "could not be found.\n";
}
EventType* EventType::getByID(std::string id) {
foreach(EventType * event, EventType::allEvents) {
if(event->akaID == id) {
return event;
}
}
std::cout << "Error: Event with aka.ID " << id << "could not be found.\n";
}
void EventType::setAllEventTypes(std::unordered_set<EventType*> events) {
EventType::allEvents = events;
}
Now I'm getting the LNK2001
-error:
eventtype.obj : error LNK2001: unresolved external symbol ""private: static class std::unordered_set<class EventType *,struct std::hash<class EventType *>,struct std::equal_to<class EventType *>,class std::allocator<class EventType *> > EventType::allEvents" (?allEvents@EventType@@0V?$unordered_set@PEAVEventType@@U?$hash@PEAVEventType@@@std@@U?$equal_to@PEAVEventType@@@3@V?$allocator@PEAVEventType@@@3@@std@@A)".
I get this error even when I'm not using any of the static methods from outside my EventType-class. Why does this happen? Shouldn't EventType be able to link to itself?
You declared allEvents
but did not define it, you need to do that in your source file:
std::unordered_set<EventType*> EventType::allEvents;