Странная ошибка компоновщика со статическим 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" (?_myMap@MyClass@@0V?$map@HHU?$less@H@std@@V?$allocator@U?$pair@$$CBHH@std@@@2@@std@@A) test22.obj test22
23
задан BlaBla 27 August 2010 в 14:21
поделиться

1 ответ

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

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

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

30
ответ дан 29 November 2019 в 02:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: