【问题标题】:Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'未定义模板 'std::basic_string<char, std::char_traits<char>, std::allocator<char>>' 的隐式实例化
【发布时间】:2021-10-09 00:31:34
【问题描述】:

在我一直在做的项目中,我们必须将 Cocoa 通知从 C++ 子项目发送到它上面的主项目。为此,我们构建了一个映射来充当通知的 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> > > > *')

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

【问题讨论】:

  • 我的第一个猜测是你需要包含一些东西。可能是对(实用程序)或字符串的标题。
  • 我也猜string
  • 我不认为字符串头是罪魁祸首,初始化 userInfo 的行 (std::map&lt;std::string, std::string&gt; *telemetry = new std::map&lt;std::string, std::string&gt;;) 不会抛出任何错误。

标签: c++ ios objective-c++


【解决方案1】:

您需要包含此标头:

#include <string>

【讨论】:

  • 如果这不起作用,请添加:#include &lt;fstream&gt;
  • #include 就足够了。
【解决方案2】:

尝试在下面添加标题,

#include &lt;sstream&gt;

https://www.cplusplus.com/reference/sstream/stringstream/stringstream/

【讨论】:

  • 您能解释一下stringstream 类与 OP 的问题有何关联吗?
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 谢谢!为我工作。
  • @AdrianMole 这与一群收到相同错误消息的人有关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
相关资源
最近更新 更多