“cout& endl”和“提升”有什么问题?

时间:2014-03-15 05:54:03

标签: c++ boost

这是我第一次在我的机器上使用boost - Ubuntu 12.04 amd64(使用g ++ 4.6.3)。 以下是来源:

#include <boost/timer.hpp>

using namespace boost;

int main()
{
  timer t;

  cout << "max timespan: "
       << t.elapsed_max() / 3600 << "h" << endl;

  cout << "min timespan: "
       << t.elapsed_min() << "s" << endl;

  cout << "now time elapsed:"
       << t.elapsed() << "s" << endl;

  return 0;

}

然而,当我用g++ timer_test.c -o timer_test编译它时,会出现奇怪的错误:

timer_test.cpp: In function ‘int main()’:
timer_test.cpp:9:3: error: ‘cout’ was not declared in this scope
timer_test.cpp:10:44: error: ‘endl’ was not declared in this scope

然后我尝试将coutendl更改为std::coutstd::endl,错误变为:

error: ‘cout’ is not a member of ‘std’
error: ‘endl’ is not a member of ‘std’

2 个答案:

答案 0 :(得分:3)

您需要包含iostream标头并使用std::cout and std::endl,因为它们是在std命名空间中定义的。

#include <iostream>

std::cout << "max timespan: "
   << t.elapsed_max() / 3600 << "h" << std::endl;

与其他coutendl

相同

答案 1 :(得分:1)

在文件顶部的#include <iostream>中插入。

基本的东西真的