将2个字符串连接在一起的程序

Gzd2003 Gzd2003 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
要求编写程序将2个字符串连接在一起,程序如下:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string s1="abc",s2="def";
copy(s2,s2.size(),s1+s1.size());
cout<<s1<<endl;
}
编译出错,信息如下:
--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
CopyTest.cpp
D:\Test\CopyTest.cpp(7) : error C2782: 'class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const _E)' : template parameter '_E' is ambiguous
could be 'unsigned int'
or 'char'
D:\Test\CopyTest.cpp(7) : error C2784: 'class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const _E *)' : could not deduce template argument for 'const class std::basic_string<_E,_Tr,_A> &' from 'cl
ass std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
D:\Test\CopyTest.cpp(7) : error C2784: 'class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const _E,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const ' from 'class std::basic_string<char,struct std:
:char_traits<char>,class std::allocator<char> >'
D:\Test\CopyTest.cpp(7) : error C2784: 'class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const _E *,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const *' from 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >'
D:\Test\CopyTest.cpp(7) : error C2784: 'class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'const class std::bas
ic_string<_E,_Tr,_A> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
D:\Test\CopyTest.cpp(7) : error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std::basic_string<ch
ar,struct std::char_traits<char>,class std::allocator<char> >'
D:\Test\CopyTest.cpp(7) : error C2676: binary '+' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
D:\Test\CopyTest.cpp(7) : error C2782: '_OI __cdecl std::copy(_II,_II,_OI)' : template parameter '_II' is ambiguous
could be 'unsigned int'
or 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
Error executing cl.exe.

Test.exe - 8 error(s), 0 warning(s)

错在哪里?请指教。谢谢。
...全文
评论 17
收藏 已收藏
分享
返回顶部
Gzd2003
C++ 语言 65,214 成员 250,514 内容