"cout&endl"和"boost"有什么问题?

what's wrong with the "cout&endl" and the "boost"?

本文关键字:什么 问题 cout endl boost      更新时间:2023-10-16

这是我第一次在我的机器上使用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’

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

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

与其他cout, endl相同

#include <iostream>文件的顶部。

基本的东西