72

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.

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:

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

..and it cannot find the function for 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.

3
  • 3
    My first guess is that you need to include something. Possibly the header for pair ( utility ) or for string.
    – Kindread
    Oct 7, 2013 at 11:22
  • 1
    I would guess string too
    – doctorlove
    Oct 7, 2013 at 11:26
  • I don't think the string header is the culprit, the line where userInfo is initialised (std::map<std::string, std::string> *telemetry = new std::map<std::string, std::string>;) does not throw any errors.
    – Andrew
    Oct 7, 2013 at 11:49

2 Answers 2

146

You need to include this header:

#include <string>
2
  • 11
    If that doesn't work please add: #include <fstream> May 17, 2019 at 6:07
  • 1
    #include <string> is sufficient.
    – Mircea
    Jul 10, 2020 at 13:22
38

I got the same error trying to use the std::stringstream class. Add this header file:

#include <sstream>
5
  • 1
    Can you explain how the stringstream class is relevant to the OP's question? Oct 8, 2021 at 17:15
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Oct 8, 2021 at 20:22
  • 4
    @AdrianMole It's relevant to a bunch of people who get the same error message
    – Winter
    Apr 9, 2022 at 3:12
  • 3
    This resolved the error error: implicit instantiation of undefined template 'std::basic_stringstream<char>' Jul 24, 2022 at 5:30
  • Oh yes, just missing the header. Jul 22 at 10:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.