【发布时间】:2021-12-18 14:42:19
【问题描述】:
用clang++ -std=c++17 -g source.cpp编译sn-p
#include <map>
#include <string>
int main()
{
std::map<std::string, int> m;
m["foo"] = 23;
}
尝试使用键 foo 访问元素,但出现错误:
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x000000010000131e a.out`main at source.cpp:8:3
5 {
6 std::map<std::string, int> m;
7 m["foo"] = 23;
-> 8 }
Target 0: (a.out) stopped.
(lldb) p m["foo"]
error: no viable overloaded operator[] for type 'std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int> > >'
candidate function not viable: no known conversion from 'const char [4]' to 'const std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int> > >::key_type' (aka 'const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') for 1st argument
candidate function not viable: no known conversion from 'const char [4]' to 'std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int> > >::key_type' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') for 1st argument
(lldb) p m[std::string("foo")]
error: no member named 'string' in namespace 'std'
(lldb) p m[std::__1::string("foo")]
error: no matching conversion for functional-style cast from 'const char [4]' to 'std::__1::string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >')
candidate constructor not viable: no known conversion from 'const char [4]' to 'const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::allocator_type' (aka 'const std::__1::allocator<char>') for 1st argument
...
candidate constructor not viable: no known conversion from 'const char [4]' to 'std::initializer_list<char>' for 1st argument
candidate constructor not viable: requires 0 arguments, but 1 was provided
...
candidate constructor not viable: requires 4 arguments, but 1 was provided
如何使用 lldb 以正确的方式处理字符串变量?
【问题讨论】:
-
我无法检查这一点,但也许使用字符串文字会有所帮助?那就是添加
using namespace std::literals;并使用"foo"s:en.cppreference.com/w/cpp/string/basic_string/operator%22%22s -
@R2T2 我不知道该怎么说 lldb 才能理解
"foo"s表达式。(lldb) expr using namespace std::literals;不会改变这一点;错误error: invalid suffix on literal; C++11 requires a space between literal and identifier。 (嗯,C++11 注释从何而来?)