从一个教材的错误程序说起.docVIP

  • 3
  • 0
  • 约1.56万字
  • 约 15页
  • 2016-12-03 发布于贵州
  • 举报
从一个教材的错误程序说起

从一个教材上的错误程序说起... 1、例2-3:变量的定义与使用(P31) //examplech203.cpp main() { int a,b,c,x; unsigned int u; float u1; double u2; a=12; b=-18; u1=51.8; u2=58.8; c=28; u=b+c; x=a+b; u2=u1+u2; coutx=xendl; coutu=uendl; coutu2=u2endl; } Compiling... examplech203.cpp e:\examplech203.cpp(10) : warning C4305: = : truncation from const double to float e:\examplech203.cpp(16) : error C2065: cout : undeclared identifier e:\examplech203.cpp(16) : error C2297: : illegal, right operand has type char [3] e:\examplech203.cpp(16) : error C2065: endl : undeclared identifier e:\examplech203.cpp(17) : error C2297: : illegal, right operand has type char [3] e:\examplech203.cpp(18) : error C2297: : illegal, right operand has type char [4] e:\examplech203.cpp(19) : warning C4508: main : function should return a value; void return type assumed Error executing cl.exe. examplech203.exe - 5 error(s), 2 warning(s) 2、P139第8题:分析下面程序的运行结果: //xt408.cpp #include iostream.h #include string.h #include stdlib.h class CString { char *str; public: CString(char *s); CString(const CString t); ~CString() { cout1234567890endl; if(str!=NULL) delete[] str; } void SetString(char *s); void Display() { coutstrendl; } }; CString::CString(char *s) { str=new char[strlen(s)+1]; if(!str) { cerrAllocationg Errorendl; exit(-1); } strcpy(str,s); } CString::CString(const CString t) { str=new char[strlen(t.str)+1]; if(!str) { cerrAllocationg Errorendl; exit(-1); } strcpy(str,t.str); } void CString::SetString(char *s) { delete[] str; str=new char[strlen(s)+1]; if(!str) { cerrAllocationg Errorendl; exit(-1); } strcpy(str,s); } CString GetString(CString t) { char s[30]; t.Display(); coutPlease Input the string: ; cins; t.SetString(s); return t; } void main() { CString C1(hello); C1.Display(); CString C2=GetString(C1); C2.Display(); } 授人以鱼,不如授人以渔。 (1)为什么需要#include stdlib.h?stdlib.h有什么用? 解决办法:取消它看看编译会出现什么错误?error C2065: exit : undeclared identifie

文档评论(0)

1亿VIP精品文档

相关文档