我的计算机科学教授要我们找到cout
的声明。我使用 g++ 和 -E 参数编译了一个简单的 Hello world 程序。这是我的 hello.cpp 的样子:
#include <iostream>
using namespace std;
int main(){
string name="";
cout << "Good morning! What's your name?";
cin >> name;
cout << "Hello " << name << ".\n";
return 0;
}
我的编译命令:
g++ -E hello.cpp > hello.p
在 hello.p 中,我在 VIM 中进行了搜索,如下所示:
:/cout
我看到以下行:
extern ostream cout;
那是cout
的声明,cout
是ostream
类的一个实例吗?
编辑:
那里的wcout
声明是做什么用的?如果我没记错的话,字母“w”代表“宽”,但我不知道它有什么含义。什么是 wcout
和 wostream
?
最佳答案
是的,这确实是std::cout
的声明, 在 <iostream>
中找到标题。
相关标准件参见§27.4.1 [iostream.objects.overview]
:
Header
<iostream>
synopsis
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
namespace std {
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}
p1 The header
<iostream>
declares objects that associate objects with the standard C streams provided for by the functions declared in<cstdio>
(27.9.2), and includes all the headers necessary to use these objects.
关于c++ - cout在哪里声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9652374/