0

我正在尝试在 VC++ (VStudio 2003) 中使用 stringstream 对象,但是当我使用重载的 << 运算符尝试设置一些操纵器时出现错误。

我正在尝试以下操作:

int SomeInt = 1;  
stringstream StrStream;  
StrStream << std::setw(2) << SomeInt;  

这将无法编译(错误 C2593:'operator <<' 不明确)。
VStudio 2003 是否支持以这种方式使用操纵器?
我知道我可以直接在字符串流对象上设置宽度,例如 StrStream.width(2);
我想知道为什么更常用的方法不起作用?

4

3 回答 3

1

你确定你包含了所有正确的标题吗?以下在 VS2003 中为我编译:

#include <iostream>
#include <sstream>
#include <iomanip>

int main()
{
   int SomeInt = 1;
   std::stringstream StrStream;
   StrStream << std::setw(2) << SomeInt;
   return 0;
}
于 2008-09-16T06:08:56.017 回答
1

我喜欢这个参考网站来解决这样的流问题。

/艾伦

于 2008-09-16T06:11:33.637 回答
0

您可能只是忘记包含 iomanip,但我不能确定,因为您没有包含完整程序的代码。

这个完整的程序在这里使用 VS 2003 运行良好:

#include <sstream>
#include <iomanip>

int main()
{
    int SomeInt = 1;
    std::stringstream StrStream;
    StrStream << std::setw(2) << SomeInt;
}
于 2008-09-16T06:21:58.590 回答