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

74

在我正在工作的项目中,我们需要将来自C++子项目的Cocoa通知发送到其上方的主项目。为此,我们构建了一个地图,作为通知的userInfo字典的键值存储。

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

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);

但是,当这段代码复制到另一个子项目中的相同文件中时,编译器会在userInfo->insert调用上抛出以下异常:

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

...它找不到PostCocoaNotification函数:

No matching function for call to 'PostCocoaNotification'

此外,它在系统标头中抛出以下错误:

/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> > > > *')

我不知道我做了什么导致这样的混乱,特别是当代码在另一个子项目中完美运行(成功发送通知)时。对于问题的任何见解都将非常受欢迎。


3
我猜您需要添加一些内容。可能是对“pair(utility)”或者“string”的标题。 - Kindread
1
我猜也是 string - doctorlove
我认为字符串头并不是问题所在,初始化userInfo的那一行代码 (std::map<std::string, std::string> *telemetry = new std::map<std::string, std::string>;) 没有抛出任何错误。 - Andrew
2个回答

146

你需要包含这个标头:

#include <string>

12
如果那不起作用,请添加:#include <fstream> - Wilson Chen
1
#include <string> 就足够了。 - Mircea

41

我试图使用std::stringstream类时遇到了同样的错误。请添加这个头文件:

#include <sstream>

1
你能解释一下 stringstream 类与 OP 的问题有什么关系吗? - Adrian Mole
您的答案可以通过提供额外的支持信息来改进。请编辑以添加更多细节,例如引用或文档,以便他人可以确认您的答案是否正确。您可以在帮助中心中找到有关撰写良好答案的更多信息。 - Muhammedogz
4
@AdrianMole 这与许多人收到相同的错误消息有关。 - Winter
3
这解决了错误 error: implicit instantiation of undefined template 'std::basic_stringstream<char>' - Arundale Ramanathan
哦,是的,只是缺少标题。 - Sunding Wei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接