使用 stl 映射时出现链接器错误
我收到以下链接器错误:
错误 1 错误 LNK2001:无法解析的外部符号“私有:静态类 std::map,类 std::allocator >,struct SDL_Surface *,struct std::less,类 std::allocator > >,类std::allocator,类 std::allocator > const ,struct SDL_Surface *> > CSurface::loadedSurfaces" (?loadedSurfaces@CSurface@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAUSDL_Surface@@U?$less@V? $basic_string@DU?$char_traits@ D@std@@V?$分配器@D@2@@std@@@2@V?$分配器@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V? $分配器@D@2@@std@@PAUSDL_Surface@@@std@@@2@@std@@A) CSurface.obj
CSurface编译单元的头文件和cpp文件的代码位于:
什么导致这个链接器错误发生:(它让我发疯。
I am getting the following linker error:
Error 1 error LNK2001: unresolved external symbol "private: static class std::map,class std::allocator >,struct SDL_Surface *,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > const ,struct SDL_Surface *> > > CSurface::loadedSurfaces" (?loadedSurfaces@CSurface@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAUSDL_Surface@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAUSDL_Surface@@@std@@@2@@std@@A) CSurface.obj
The code for the header and cpp file for the CSurface compilation unit is at:
What is causing this linker error to happen :( its driving me mad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已声明
loadedSurfaces
但尚未定义它。您需要将以下内容添加到一个翻译单元中才能实际声明该变量:就像现在一样,类定义中的
loadedSurfaces
就像函数的原型。当您尝试使用它时,链接器会查找它,因为它看到了前向声明,但它永远找不到任何地方的实际定义。您必须给它一个定义,链接器就会满意,因为它知道每个人在使用名称loadedSurfaces
时正在谈论的实际翻译单元中的实际变量。You have declared
loadedSurfaces
but you have not defined it. You need to add the following to exactly one translation unit to actually declare the variable:As it is now, the
loadedSurfaces
inside the class definition is like the prototype of the function. When you try to use it, the linker goes and looks for it because it sees the forward declaration, but it never finds the actual definition anywhere. You have to give it a definition and the linker will be satisfied because it knows what actual variable in what actual translation unit everyone is talking about when they use the nameloadedSurfaces
.CSurface::loadedSurfaces
已声明但未定义。将以下内容添加到.cpp
文件中:CSurface::loadedSurfaces
is declared but not defined. Add the following to the.cpp
file: