C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)

简介: C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)

练习c++的输入输出时,编译遇到错误:


【error: 'cout' was not declared in this scope

   error: 'cin' was not declared in this scope】

1.png



原错误代码如下:

#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;
}

编译无错误,运行截图如下:


1.png


参考文章:【https://www.runoob.com/cplusplus/cpp-basic-input-output.html


             https://blog.csdn.net/fsz520w/article/details/82561795


相关文章
|
3月前
|
编译器 开发工具 C++
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
772 0
|
2月前
|
测试技术 C++
【C++】解决googleTest报错error: SEH exception with code 0xc0000005 thrown in the test body.
【C++】解决googleTest报错error: SEH exception with code 0xc0000005 thrown in the test body.
|
2月前
|
JavaScript C++
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
|
4月前
|
Linux C++
经验分享:C++ error:‘syscall’ was not declared in this scope
经验分享:C++ error:‘syscall’ was not declared in this scope
109 3
|
5月前
|
编译器 程序员 C++
【C/C++ 泛型编程 进阶篇】C++模板推导的迷宫:导致编译错误的原因及其解决策略
【C/C++ 泛型编程 进阶篇】C++模板推导的迷宫:导致编译错误的原因及其解决策略
99 2
|
5月前
|
C++
C++系列-第1章顺序结构-3-输出类cout
C++系列-第1章顺序结构-3-输出类cout
|
11月前
|
Linux C++
【C++编译】C++ error:‘syscall’ was not declared in this scope
sys/syscall.h 内部表示,他封装了![[Pasted image 20220818151746.png]] 打开对应的 syscall.h 文件内部依旧没有 syscall()函数的声明。
91 0
|
12月前
|
缓存 C++
C++的输入与输出:cin与cout
C++的输入与输出:cin与cout
|
IDE 编译器 开发工具
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
|
传感器 IDE 开发工具
排错实战 —— 解决 c++ 工程编译错: error C2059 'string' illegal token on right
排错实战 —— 解决 c++ 工程编译错: error C2059 'string' illegal token on right