c++ - 为什么 g++ 在编译时给我冲突错误?
问题描述
我正在尝试编译我的第二个(仍然是 noobish)C++ 程序,而 g++ 给了我这些错误:
new.cpp: In function ‘int main()’:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin >> name;
是第一个。这是第二个:
^~~
new.cpp:10:4: note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
我相信这些告诉我要改变两种方式来将它写给另一个。我已经尝试更改两者,但我不确定如何解决这个问题。这是程序:
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
cin >> name;
return 0;
}
(关闭)
解决方案
以下是对 c++ 和 g++ 新手的一点解释:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin
在命名空间下声明std
。见https://en.cppreference.com/w/cpp/io/cin
第二个不是错误,而是编译器通过指向编译器找到的替代方案提出的建议。它给出了关于std::cin
.
note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
在第 10 行,您正在使用cin
全局命名空间。因此,编译器抱怨它找不到cin
.
我们的同事已经通过将第 10 行更改为:std::cin >> name;
.
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
std::cin >> name;
return 0;
}
推荐阅读
- c++ - C++ cout 和 endl 无法在终端上正确打印
- java - IClientConfig 参数的用途是什么?
- json - PostgreSQL 9.5:将 json_agg 结果组合成单个 json 对象
- python - 在python中并行执行'N'个函数
- excel - 基于单元格值隐藏行非常慢
- javascript - 编译和缩小时让 child_process.fork() 在电子中工作
- php - 循环遍历数组并按索引添加到另一个数组
- java - 以用户的设备格式和语言显示日期和时间
- java - 无法在 Spring Boot 中使用 Crud Repository 从 Redis 获取结果?
- php - Laravel 使用 auth()->user() 作为 api