首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

初学者一问

2012-02-14 
菜鸟一问照例题输入,结果报错,请大家帮忙看看:#includeiostream.hstruct couple{char man[5]char woman

菜鸟一问
照例题输入,结果报错,请大家帮忙看看:
#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)



[解决办法]

C/C++ code
//#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'; } 

热点排行