2010-08-27 14 views
20

क्यों जब मैं दृश्य स्टूडियो में इस संकलन करने की कोशिश मैं लिंकर त्रुटि मिलता है 2008स्थिर एसटीडी के साथ अजीब लिंकर त्रुटि :: नक्शा

#include <stdafx.h> 
#include <iostream> 
#include <map> 
#include <string> 

class MyClass 
{ 
public: 
MyClass() { }; 
virtual ~MyClass() {}; 

static std::string niceString (std::map<int, int> mappp) { _myMap = mappp; return "nice string"; }; 

private: 
static std::map<int, int> getMap () { return _myMap; }; 

static std::map<int, int> _myMap; 
}; 

int main(){ 

std::map<int, int> mappp; 

mappp[1] = 1; 

std::cout << MyClass::niceString(mappp); 

} 

त्रुटि है:

Error 1 error LNK2001: unresolved external symbol "private: static class std::map<int,int,struct std::less<int>,class std::allocator<struct std::pair<int const ,int> > > MyClass::_myMap" ([email protected]@@[email protected][email protected]@[email protected]@[email protected][email protected][email protected]@@@[email protected]@[email protected]@A) test22.obj test22 
+1

अनिवार्य रूप से [स्टेटिक स्ट्रक्चर लिंकर त्रुटि] का एक डुप्लिकेट (http://stackoverflow.com/questions/1850809/static-struct-linker-error) दूसरों के बीच। – Troubadour

+0

संभावित डुप्लिकेट [सी ++ में स्थिर सदस्यों को परिभाषित करना] (http://stackoverflow.com/questions/3536372/defining-static-members-in-c) –

उत्तर

26

आप घोषित किया है स्थैतिक सदस्य _myMap, लेकिन इसे परिभाषित नहीं किया गया। एक समारोह है कि घोषित किया गया है, लेकिन किसी भी .cpp फ़ाइल में परिभाषित नहीं की तरह

std::map<int, int> MyClass::_myMap; 

इसके बारे में सोचते हैं - आप एक लिंकर त्रुटि मिलती है अगर आप इसका इस्तेमाल: सिर्फ int main() ऊपर इस लाइन में जोड़ें।

+0

यह भी आवश्यक है यदि मेरे पास एक और स्थिर चर है, उदा। std :: स्ट्रिंग myString? – BlaBla

+0

@ बालाबाला: हाँ ... –

+0

मुझे लगता है कि आपका मतलब यह नहीं था "लेकिन इसे परिभाषित नहीं किया गया", लेकिन इसके बजाय "लेकिन इसे प्रारंभ नहीं किया गया" – sergiol

संबंधित मुद्दे