错误消息:使用g ++“未定义setw”

时间:2013-09-24 13:59:57

标签: c++

我正在尝试使用gcc编译一个早先使用过SunStudio的项目,并在以下代码中出现错误:

ostream & operator << ( ostream & os, const UtlDuration & d )
{
    if ( d._nsec == 0 )
    {
        os << d._sec << " sec";
        return os;
    }
    else
    {
        cout.fill( '0' );
                os << d._sec << "." << std::setw(9) << d._nsec << " sec";
        cout.fill( ' ' );
        return os;
    }
}

错误:“setw”不是“std”的成员

我无法解决此错误,有人可以解释一下这个错误背后的原因

2 个答案:

答案 0 :(得分:47)

您需要包含声明它的标题:

#include <iomanip>

答案 1 :(得分:1)

执行以下两个步骤:

  1. 包括iomanip
  2. 写std :: setw()而不是setw()

编译并享受...