为什么我不能从C中的单个char构造一个std :: string?
发布时间:2020-12-16 10:44:36 所属栏目:百科 来源:网络整理
导读:从char创建一个std :: string.例如,不会编译, #include stringint 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:
从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,_All oc>::basic_string(std::__cxx11::basic_string<_CharT,_Alloc>::size_type,_CharT,const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT,_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,_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,std::__cxx11::basic_string<_CharT,_Al loc>::size_type,const _Alloc&) [with _CharT = char; _Traits = std::char_traits< char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT,_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,_All oc>::basic_string(const std::__cxx11::basic_string<_CharT,_Alloc>&,st d::__cxx11::basic_string<_CharT,std::__cxx11::basi c_string<_CharT,const _Alloc&) [with _CharT = char ; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11: :basic_string<_CharT,_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,_Alloc>::size_type) [with _CharT = char; _Traits = std ::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_ CharT,^ 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,_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,_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,_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 const s{ 'A' }; 关于这不受支持的谣言是夸大其词. 从技术上讲,这使用了initializer_list构造函数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |