c++ - 为什么我不能在 C++ 中从单个字符构造 std::string?

标签 c++ c++11 stdstring

char 生成std::string。例如,不会编译,

#include <string>

int main()
{
    char c = 'a';
    std::string s(c);
    return 0;
}

在 g++ 中,我得到这个很多错误,

foo.cpp: In function 'int main()':
foo.cpp:6:20: error: no matching function for call to 'std::__cxx11::basic_strin
g<char>::basic_string(char&)'
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note: candidate: template<class _InputIterator> std::__cxx11::bas
ic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator,
 const _Alloc&)
         basic_string(_InputIterator __beg, _InputIterator __end,
         ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note:   template argument deduction/substitution failed:
foo.cpp:6:20: note:   candidate expects 3 arguments, 1 provided
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:465:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type
, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Allo
c>::size_type = unsigned int]
       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:465:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:455:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = s
td::char_traits<char>; _Alloc = std::allocator<char>] <near match>
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:455:7: note:   conversion of argument 1 would be ill-formed:
foo.cpp:6:20: error: invalid conversion from 'char' to 'const char*' [-fpermissi
ve]
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:445:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Al
loc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<
char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits
, _Alloc>::size_type = unsigned int]
       basic_string(const _CharT* __s, size_type __n,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:445:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:427:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, st
d::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basi
c_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char
; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11:
:basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:427:7: note:   candidate expects 4 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:411:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, st
d::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basi
c_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std
::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_
CharT, _Traits, _Alloc>::size_type = unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:411:7: note:   candidate expects 3 arguments, 1 provided
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:399:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [w
ith _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<cha
r>]
       basic_string(const basic_string& __str)
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:399:7: note:   no known conversion for argument 1 from 'char' to 'const
std::__cxx11::basic_string<char>&'
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:391:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits
<char>; _Alloc = std::allocator<char>]
       basic_string(const _Alloc& __a)
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:391:7: note:   no known conversion for argument 1 from 'char' to 'const
std::allocator<char>&'
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:380:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _All
oc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Allo
c = std::allocator<char>]
       basic_string()
       ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:380:7: note:   candidate expects 0 arguments, 1 provided

是否有理由缺少此构造函数。

这行得通,但感觉不对,

#include <string>

int main()
{
    char c = 'a';
    std::string s;
    s += c;
    return 0;
}

任何建议,谢谢。

最佳答案

string 不是 char,所以我不知道为什么您会期望一个可以转换为另一个。前者是后者的集合。事实上,理由是相反的方向——你需要证明这样一个构造函数存在的理由,但实际上没有。从 charstring 的隐式转换会非常非常奇怪。

如果要单个元素的集合,有constructors为此:

std::string s1(1, c); // (2) create a string composed of 1 repeated character c
std::string s2{c};    // (9) create a string based on this sequence of chars

关于c++ - 为什么我不能在 C++ 中从单个字符构造 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163032/

相关文章:

c++ - 如何使用boost计算文件的md5?

c++ - 声明现有原始指针所有权的正确方法

c++ - 如何推断派生类的类型

c++ - automake undefined reference

c++ - C++/CX 中的 [this] 是什么? (Windows 8)

c++ - Boost 文件系统与 c++11 线程不兼容

C++:将 std::string 传递给要更改字符串的 C 函数的正确实现?

c++ - 在 std::string 中存储 unicode UTF-8 字符串

c++ - 为我的字符串类实现 reverse_iterator(还有 rbegin() 和 rend() 方法)

c++ - 时间复杂度 - 大 O - 显示不在文件中的间隔中的数字