ostringstream 运算符<<很长时间?

发布于 2024-12-01 18:21:30 字数 1222 浏览 0 评论 0 原文

$ uname -a
Darwin Wheelie-Cyberman 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat nolove.cc
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char ** argv) {
  unsigned long long i = 0;
  ostringstream o();

  // Compiles fine
  cout << i;

  // Explodes, see below
  o << i;

  return 0;
}

$ g++ -o nolove nolove.cc
nolove.cc: In function ‘int main(int, char**)’:
nolove.cc:14: error: invalid operands of types ‘std::ostringstream ()()’ and ‘long long unsigned int’ to binary ‘operator<<’

我对 C++ 有点陌生(但对编程或面向对象设计等不熟悉),所以我假设我只是做错了。实际上,上面的 unsigned long long 相当于我的目标平台上的无符号 64 位整数(上面和 linux 2.6 上的 g++ 4.4.1),相当于相同事物的不同类型也是可以接受的(但我还没有找到任何.)

我可以使用 ostringstream 来格式化此(或类似)类型吗?如果没有,我可以在不拖入 stdio 和 snprintf 的情况下完成吗?从更哲学的角度来说,cout 可以执行的类型是如何实现的,以及为什么该功能没有扩展到字符串流内容?

$ uname -a
Darwin Wheelie-Cyberman 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat nolove.cc
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char ** argv) {
  unsigned long long i = 0;
  ostringstream o();

  // Compiles fine
  cout << i;

  // Explodes, see below
  o << i;

  return 0;
}

$ g++ -o nolove nolove.cc
nolove.cc: In function ‘int main(int, char**)’:
nolove.cc:14: error: invalid operands of types ‘std::ostringstream ()()’ and ‘long long unsigned int’ to binary ‘operator<<’

I'm somewhat new to C++ (but not to programming or OO design, etc) so I'm assuming I'm just doing it wrong. In practice the unsigned long long above equates to an unsigned 64bit integer on my targeted platforms (above and g++ 4.4.1 on linux 2.6), a different type that amounted to the same thing would also be acceptable (but I haven't found any.)

Can I use an ostringstream to format this (or similar) type? If not, can I do it without dragging in stdio and snprintf? More philosophically, how does the typing work out that cout can do it, and why wasn't that functionality extended to the string stream stuff?

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

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

发布评论

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

评论(1

庆幸我还是我 2024-12-08 18:21:30

这是因为这

ostringstream o(); 

没有声明变量,而是声明一个返回流的函数。

试试这个 另

ostringstream o; 

请参阅

最令人烦恼的解析:为什么不 A a(() );工作?

This is becuse this

ostringstream o(); 

doesn't declare a variable, but a function returning a stream.

Try this instead

ostringstream o; 

See also

Most vexing parse: why doesn't A a(()); work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文