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

Gzd2003 2008-11-27 09:52:27
要求编写程序将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)

错在哪里?请指教。谢谢。
...全文
399 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxianrong 2008-11-27
  • 打赏
  • 举报
回复
s1=s1+s2;

LS的那样可以么??
ChamPagneZ 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lann64 的回复:]
s1=s1+s2;
[/Quote]
UP.
搞COPY的测试应该是这样:

#include <iostream>
#include <string>
#include <iterator>
using namespace std;
int main(){
string s1="DN",s2="CS";
copy(s1.begin(),s1.end(),back_inserter(s2));
cout << s2 << endl;
return 0;
}
cyj626 2008-11-27
  • 打赏
  • 举报
回复
string 可以直接++的
BaihowFF 2008-11-27
  • 打赏
  • 举报
回复
既然是string这种C++的东西 直接用加号就可以了啊...
Super.Jiju 2008-11-27
  • 打赏
  • 举报
回复
void main()
{
string s1="abc",s2="def";
copy(s2,s2.size(),s1+s1.size());
cout < <s1 < <endl;
}

要么就s1+=s2;
要么就重新申请一个空间
lann64 2008-11-27
  • 打赏
  • 举报
回复
s1=s1+s2;
lgccaa 2008-11-27
  • 打赏
  • 举报
回复
用strncpy吧,不要用copy,除非是自己写的实现功能的函数
xxgamexx 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 s79947171 的回复:]
楼上的最后应该手动加上/0吧?
[/Quote]

不用了 已经赋值了
s79947171 2008-11-27
  • 打赏
  • 举报
回复
楼上的最后应该手动加上/0吧?
xxgamexx 2008-11-27
  • 打赏
  • 举报
回复
我也写了一个



#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
void stradd(char *head,const char *tail,int size)
{
assert(head!=NULL||tail!=NULL);
while((size--)!=0)*head++;
while((*head++=*tail++)!='\0');
}
int main()
{

char a[20]="hello";
char b[10]=" world!";
stradd(a,b,strlen(a));
cout<<a;
return 0;
}
  • 打赏
  • 举报
回复
哎。用C++写还这么费劲干吗,重载的运算符,用了省事。
ugrg 2008-11-27
  • 打赏
  • 举报
回复
s1+s1.size() 这个有些问题吧
General1982 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 Cricketol 的回复:]
string已经重载了+操作符了,直接s1 += s2;就得了
[/Quote]

正解!!
xianyuxiaoqiang 2008-11-27
  • 打赏
  • 举报
回复
既然用string,那就直接s1+=s2;
否则,可以自己实现之:


#include "string.h"
char * stradd(char **dest,const char *src)
{
char *temp = NULL;
int len_dest = 0;
int len_src = 0;
int index = 0;
int i = 0;
if(dest == NULL)return NULL;
if(src == NULL || *src == '\0')
{
return *dest;
}
len_src = strlen(src);
if(*dest == NULL)
{
*dest = new char[len_src + 1];
strcpy(*dest,src);
}
else
{
len_dest = strlen(*dest);
temp = new char[len_src + len_dest +1];
while((*dest)[i] != '\0')
{
temp[index] = (*dest)[i];
i++;
index++;
}
i = 0;
while(src[i] != '\0')
{
temp[index] = src[i];
i++;
index++;
}
temp[index] = '\0';
delete [](*dest);
*dest = temp;
}
return *dest;
}
int main(void)
{
char *a = new char[10];
a[0]='a';
a[1]='\0';
char *b = "bcd";
stradd(&a,b);
printf(a);
}

Cricketol 2008-11-27
  • 打赏
  • 举报
回复
string已经重载了+操作符了,直接s1 += s2;就得了
zhanshen2891 2008-11-27
  • 打赏
  • 举报
回复
自己看一下书!
yinghui130 2008-11-27
  • 打赏
  • 举报
回复



支持6楼



引用 2 楼 lann64 的回复:
s1=s1+s2;

UP.
搞COPY的测试应该是这样:

C/C++ code

#include <iostream> #include <string> #include <iterator> using namespace std; int main(){ string s1="DN",s2="CS"; copy(s1.begin(),s1.end(),back_inserter(s2)); cout << s2 << endl; return 0; }

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧