C++ 未定义模板 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' 的隐式实例化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:37:23  来源:igfitidea点击:

Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'

c++iosobjective-c++

提问by Andrew

In the project I have been working on, we have to send Cocoa notifications from C++ sub-projects to the main project above it. To do this we construct a map to act as a key-value store for the userInfo dictionary of the notification.

在我一直在做的项目中,我们要从C++子项目向它上面的主项目发送Cocoa通知。为此,我们构造了一个映射作为通知的 userInfo 字典的键值存储。

In one of the projects, the following code compiles just fine:

在其中一个项目中,以下代码编译得很好:

std::map<std::string, std::string> *userInfo = new std::map<std::string, std::string>;
char buffer[255];

sprintf(buffer, "%i", intValue1);
userInfo->insert(std::pair<std::string, std::string>("intValue1", std::string(buffer)));

sprintf(buffer, "%i", intValue2);
userInfo->insert(std::pair<std::string, std::string>("intValue2", std::string(buffer)));

if(condition)
    userInfo->insert(std::pair<std::string, std::string>("conditionalValue", "true"));

PostCocoaNotification("notificationName", *userInfo);

However, when this is copied into an identical file in another sub-project, the compiler throws the following on the userInfo->insert calls:

但是,当将其复制到另一个子项目中的相同文件中时,编译器会在 userInfo->insert 调用中抛出以下内容:

"Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'" 

..and it cannot find the function for PostCocoaNotification:

..它找不到 PostCocoaNotification 的函数:

No matching function for call to 'PostCocoaNotification'

Additionally, it throws the following errors in system headers:

此外,它会在系统标头中引发以下错误:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:73:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:74:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:73:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1324:13: Cannot initialize a parameter of type '_Link_type' (aka '_Rb_tree_node<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > *') with an rvalue of type '_Const_Link_type' (aka 'const _Rb_tree_node<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > *')

I've no idea what I've done to cause such chaos, especially when the code runs perfectly fine in another sub-project (successfully sending notifications). Any insight to the problem would be very welcome.

我不知道我做了什么导致这种混乱,尤其是当代码在另一个子项目中运行得很好时(成功发送通知)。对这个问题的任何见解将是非常受欢迎的。

回答by Mauro Bilotti

You need to include these headers:

您需要包含这些标题:

#include <string>
#include <sstream>
#include <iostream>