练习c++的输入输出时,编译遇到错误:
【error: 'cout' was not declared in this scope
error: 'cin' was not declared in this scope】
原错误代码如下:
#include<stdio.h>
#include<iostream>
int main(){
float f,c; //为了输出带精度的小数
cout << "转换前的华氏温度为:";
cin>>f;
c = 5*(f-32)/9;
cout << "转换后的摄氏温度为:" <<c;
return 0;
}
不能成功运行,会报上图的错。
经查找后发现是命名空间的问题,为了将c++和c区分开,规定c++的头文件都没有后缀.h,当时用iostream的时候(预定义的对象 cin 和cout是 iostream 类的一个实例),就需要用namespace std来配合,才能使用cin或cout的这种标识符。
代码加上命名空间(更改后的正确代码):
#include<stdio.h>
#include<iostream>
//下面一行为增加的命名空间
using namespace std;
int main(){
float f,c;
cout << "转换前的华氏温度为:";
cin>>f;
c = 5*(f-32)/9;
cout << "转换后的摄氏温度为:" <<c;
return 0;
}
编译无错误,运行截图如下:
参考文章:【https://www.runoob.com/cplusplus/cpp-basic-input-output.html
https://blog.csdn.net/fsz520w/article/details/82561795】
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)