ich eine Klasse haben EventType
, die die folgenden Header (relevante Linien nur) hat:LNK2001 Fehler bei statischen Attribute und Methoden (Qt, C++)
#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;
};
und die Quelle:
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;
}
jetzt bin immer ich den LNK2001
-Fehler:
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" ([email protected]@@[email protected]@@[email protected]@@@[email protected]@[email protected]@@@[email protected][email protected]@@@[email protected]@[email protected]@A)".
ich diesen Fehler, auch wenn ich nicht eine Verwendung ny der statischen Methoden von außerhalb meiner EventType-Klasse. Warum passiert das? Sollte EventType nicht mit sich selbst verlinken können?
Dieses es gelöst. Ich wundere mich immer noch, warum, da ich neu in C++ und für nicht-statische Mitglieder bin, war es immer ausreichend, sie in der .h-Datei zu deklarieren. Vielen Dank! – Saftkeks