本文介绍了奇怪的链接器错误与静态std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么当我尝试在Visual Studio 2008中编译时会出现链接器错误
c> c> > 已经声明了静态成员 _myMap ,但是没有定义它。添加此行 int main():std :: map< int,int> MyClass :: _ myMap;
想象它是一个已声明但未在任何.cpp文件中定义的函数如果您使用链接器错误。
Why do I get linker error when I try to compile this in 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 is:
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解决方案You've declared the static member _myMap, but not defined it. Add this line just above int main():
std::map<int, int> MyClass::_myMap;Think of it like a function that has been declared but not defined in any .cpp file - you get a linker error if you use it.
这篇关于奇怪的链接器错误与静态std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!