stringstream 操纵器 & VS工作室2003

发布于 2024-07-05 09:53:40 字数 405 浏览 2 评论 0原文

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

我正在尝试以下操作:

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

这不会编译(错误 C2593:“运算符 <<”不明确)。
VStudio 2003 支持以这种方式使用操纵器吗?
我知道我可以直接在 stringstream 对象上设置宽度,例如 StrStream.width(2);
我想知道为什么更常用的方法不起作用?

I am trying to use a stringstream object in VC++ (VStudio 2003) butI am getting an error when I use the overloaded << operator to try and set some manipulators.

I am trying the following:

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

This will not compile (error C2593: 'operator <<' is ambiguous).
Does VStudio 2003 support using manipulators in this way?
I know that I can just set the width directly on the stringstream object e.g. StrStream.width(2);
I was wondering why the more usual method doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-07-12 09:53:40

您确定包含了所有正确的标头吗? 我在 VS2003 中编译的结果如下:

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

int main()
{
   int SomeInt = 1;
   std::stringstream StrStream;
   StrStream << std::setw(2) << SomeInt;
   return 0;
}

Are you sure you included all of the right headers? The following compiles for me in VS2003:

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

int main()
{
   int SomeInt = 1;
   std::stringstream StrStream;
   StrStream << std::setw(2) << SomeInt;
   return 0;
}
人生百味 2024-07-12 09:53:40

我喜欢这个参考网站来解决此类问题。

/艾伦

I love this reference site for stream questions like this.

/Allan

我家小可爱 2024-07-12 09:53:40

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

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

#include <sstream>
#include <iomanip>

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

You probably just forgot to include iomanip, but I can't be sure because you didn't include code for a complete program there.

This complete program works fine over here using VS 2003:

#include <sstream>
#include <iomanip>

int main()
{
    int SomeInt = 1;
    std::stringstream StrStream;
    StrStream << std::setw(2) << SomeInt;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文