菜鸟一问
照例题输入,结果报错,请大家帮忙看看:
#include<iostream.h>
struct couple
{
char man[5];
char woman[5];
};
void break_up(struct couple *ptr);
int main()
{
struct couple Camastra={"John","Sara"};
struct couple *ptr;
ptr=& Camastra;
cout<<"Before break_up,Camastra couple was: \n";
cout<<Camastra.man<<" "<<Camastra.woman<<"\n";
break_up(ptr);
cout<<"After break_up,Camastra couple was: \n";
cout<<Camastra.man<<" "<<Camastra.woman<<"\n";
return 0;
}
void break_up(struct couple *ptr)
{
ptr->woman='C';
}
C:\Documents and Settings\Administrator\桌面\4.cpp(23) : error C2440: '=' : cannot convert from 'const char' to 'char [5]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
执行 cl.exe 时出错.
4.obj - 1 error(s), 0 warning(s)
[解决办法]
//#include <iostream.h> 这个头文件不建议使用了#include <iostream> using namespace std;struct couple { char man[5]; char woman[5]; }; void break_up(struct couple *ptr); int main() { struct couple Camastra={"John","Sara"}; struct couple *ptr; ptr=& Camastra; cout <<"Before break_up,Camastra couple was: \n"; cout <<Camastra.man <<" " <<Camastra.woman <<"\n"; break_up(ptr); cout <<"After break_up,Camastra couple was: \n"; cout <<Camastra.man <<" " <<Camastra.woman <<"\n"; return 0; } void break_up(struct couple *ptr) { // 这是一个字符数组,你不能直接对其赋值 ptr-> woman[0]= 'C'; }