Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
9天前关闭。
Improve this question
我正在尝试编译我的第二个(仍然很笨拙的)C++程序,而g++给了我这些错误:
我们的同伴已经通过将第10行更改为
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
9天前关闭。
Improve this question
我正在尝试编译我的第二个(仍然很笨拙的)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++新手的一些解释:
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行,您正在使用全局 namespace 中的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/