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

And*_*rew 48 c++ objective-c++ ios

在我一直在研究的项目中,我们必须将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);
Run Code Online (Sandbox Code Playgroud)

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

"Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'" 
Run Code Online (Sandbox Code Playgroud)

..并且找不到PostCocoaNotification的功能:

No matching function for call to 'PostCocoaNotification'
Run Code Online (Sandbox Code Playgroud)

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

/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> > > > *')
Run Code Online (Sandbox Code Playgroud)

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

Mau*_*tti 94

您需要包含以下标题:

#include <string>
#include <sstream>
#include <iostream>
Run Code Online (Sandbox Code Playgroud)

  • 如果这不起作用,请添加:`#include &lt;fstream&gt;` (4认同)

小智 46

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

#include <sstream>
Run Code Online (Sandbox Code Playgroud)

  • @AdrianMole 这与一群收到相同错误消息的人相关 (4认同)
  • 这解决了错误“错误:未定义模板‘std::basic_stringstream&lt;char&gt;’的隐式实例化” (3认同)