- 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
您可能关注的文档
最近下载
- 音体美教学常规要求.doc VIP
- (2025)新课标《义务教育数学课程标准(2022年版)》测试题(附含答案) .pdf VIP
- 质量工程师中级培训教程.doc VIP
- 高中英语(北师大版)必修3—单词表.pdf VIP
- Unit 1 Time to Relax (第1课时) Section A 1a- 2d 课件 2026年春人教版英语八年级下册.pptx VIP
- QE质量工程师培训完整版教材.ppt VIP
- 杜曼教育——如何教宝宝学数学.pdf VIP
- 香港小学中文修辞手法练习.pdf VIP
- 2025-2026学年初中数学八年级上册(2024)沪教版(五四制)(2024)教学设计合集.docx
- GNSS接收机抗干扰技术研究.pptx VIP
原创力文档

文档评论(0)