【发布时间】:2015-08-21 23:23:47
【问题描述】:
我在使用“gedit take_input.cpp”后写了如下代码:
#include <iostream>
int main()
{
cout<<"please enter your name (followed by 'enter')\n";
string file;
cin >> file;
cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}
但是,当我使用“g++”将我的人类可读代码转换为目标代码(编写g++ take_input.cpp -o take_input)时,终端返回的结果类似于:
take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
cout<<"please enter your name (followed by 'enter')\n";
^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
string file;
^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
from /usr/include/c++/4.9/ios:38,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
cin >> file;
^ ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
^
你能告诉我是什么原因吗?
【问题讨论】:
-
你必须写
std::cout,因为它在std命名空间中。 -
顺便说一句,你可以很高兴你没有被允许发布图片。我敢肯定,如果你这样做,你会得到更多的反对票。图片不可复制/粘贴,因此当您有一些代码时,请始终将其作为文本而不是图片。
-
@SadmanSakib “我不明白。为什么我的问题被否决了?” 因为您的研究工作量很低。非常基本的教程和 C++ 教科书已经涵盖了这一点。
-
@Nivetha Please stop teaching this to beginners!
-
@SadmanSakib De nada。您也可以再次浏览The tour,并阅读Help Center 中的文章。
标签: c++ linux ubuntu gcc terminal