Подтвердить что ты не робот

Weird ошибка компоновщика со статическими std:: map

Почему я получаю ошибку компоновщика при попытке скомпилировать ее в Visual Studio 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
4b9b3361

Ответ 1

Вы объявили статический член _myMap, но не определили его. Добавьте эту строку чуть выше int main():

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

Подумайте об этом как о функции, которая была объявлена, но не определена в любом файле .cpp. Если вы используете ее, вы получите ошибку компоновщика.