2016-12-31 94 views
1

在一个旧的项目代码的安慰是如下语句:印刷使用的ostream和运营商<<

cout.operator<<("Hi...");  
ostream.operator<<("Hi...."); 

它认为它可以写成followa,导致相同的输出:

cout << "Hi.."; 

我试图把这些语句放在main()中。但是,编译器会发出错误:

In function 'int main()': 
11:3: error: 'ostream' was not declared in this scope 
11:3: note: suggested alternative: 
In file included from /usr/include/c++/4.9/ios:38:0, 
       from /usr/include/c++/4.9/ostream:38, 
       from /usr/include/c++/4.9/iostream:39, 
       from 2: 
/usr/include/c++/4.9/iosfwd:136:33: note: 'std::ostream' 
    typedef basic_ostream<char> ostream; 

,我不服气的另一件事是本声明的ostream可以直接调用操作< <。直接在cout可以呼叫运营商< <

如果有些机构可以详细说明或解释如何,那么我将能够调试代码。
谢谢。

+0

你可以[编辑]你的帖子来显示主要功能&包括? –

+0

错误消息是没有名为'ostream'的变量。将第二行更改为'cout.operator <<(“Hi ....”);'或'static_cast (cout).operator <<(“Hi ....”);'。 –

回答

0

有问题的代码线实际上可以BER编译并执行工作,例如:

#include <iostream> 

using namespace std; 

int main (void) 
{ 
    cout << "Hi"; // code line in question 

    cout << endl; 

    return 0; 
} 

上述的误差是通过使用ostream,这是cout的类型,在一个地方引起其中的对象(未一个类型)是必需的。
正如在评论中提到的,该错误的修复是使用该类型的对象。这基本上意味着引用代码的第一行,即上面解决方案代码中的行。