c++ - 为什么 g++ 在编译时给我冲突错误?

标签 c++ string compiler-errors cin

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

去年关闭。




Improve this question




我正在尝试编译我的第二个(仍然是 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

cinstd 下声明命名空间。见 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++ - 为什么 g++ 在编译时给我冲突错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65740922/

相关文章:

c++ - 为什么 "greater than"不适用于 char 类型?

ios - HackerRink 解决方案重构代码 (Swift)

c++ - 在 C++ 中将 'new' 关键字与结构一起使用

asp.net - 无法获取asp.net中只读文本框的值

c++ - 静态分配和动态分配返回不同的答案

c++ - 无法在 C++ 中使用 ReadConsoleInput 读取鼠标事件

string - 如何在 Swift 中使用索引设置字符串的字符

c# - 扩展方法必须在非泛型静态类中定义

c++ - 为什么不的unique_ptr在C++ 20 equality_comparable_with nullptr_t?

javascript - 用于将字符串拆分为数组并替换数组元素的正则表达式?