我使用带有Arduino IDE的ESP8266来处理微控制器项目。但是,我也希望以此为契机,了解有关C ++的更多信息。我试图反序列化一个字符串,如" key1:value1; key2:value2; ..."在一个字符串的地图中,我遇到了一个似乎相关的问题:Parsing a comma-delimited std::string
所以我把它放到像
这样的函数中void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
std::stringstream ss(input);
std::string key;
std::string value;
while(ss.good()) {
std::getline(ss, key, ':');
std::getline(ss, value, ';');
output->insert(std::make_pair(key, value));
}
}
但是我收到了编译错误(从IDE复制),不幸的是我无法弄清楚我需要修复什么。我确实将问题隔离到了std::stringstream ss(input);
行,但我不确定问题是什么。
Arduino: 1.6.13 (Mac OS X), Board: "Adafruit HUZZAH ESP8266, 80 MHz, 4M (3M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 921600"
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o):(.literal._ZSt9use_facetISt5ctypeIcEERKT_RKSt6locale[std::ctype<char> const& std::use_facet<std::ctype<char> >(std::locale const&)]+0x4): undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::ctype<char> const& std::use_facet<std::ctype<char> >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::numpunct<char> const& std::use_facet<std::numpunct<char> >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o):/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: more undefined references to `std::__throw_bad_cast()' follow
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o):(.literal._ZNSt6locale5facet18_S_create_c_localeERPiPKcS1_+0x4): undefined reference to `std::__throw_runtime_error(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o): In function `std::locale::facet::_S_create_c_locale(int*&, char const*, int*)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/src/c++98/c++locale.cc:207: undefined reference to `std::__throw_runtime_error(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o):(.literal._ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate[std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)]+0x4): undefined reference to `std::__throw_ios_failure(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o): In function `std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/basic_ios.tcc:122: undefined reference to `std::__throw_ios_failure(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(istream-inst.o): In function `__check_facet<std::ctype<char> >':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/istream:95: undefined reference to `std::__throw_bad_cast()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Adafruit HUZZAH ESP8266.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
提前致谢!
答案 0 :(得分:0)
感谢Zebrafish指点我this question about sstream with Arduino。显然很难在Arduino项目中包含很多std c ++,包括stringstream。
这让我找到了this answer, which recommends staying away from Arduino String。我用它来修改我的功能:
#include <string>
#include <cstring>
#include <map>
void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
char* inputChars = const_cast<char*>(input.c_str());
char* key;
char* value;
key = strtok(inputChars, ":");
value = strtok(NULL, ";");
while(key != NULL && value != NULL) {
output->insert(std::make_pair(key, value));
key = strtok(NULL, ":");
value = strtok(NULL, ";");
}
}