$ 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++有些陌生(但不是编程或OO设计等),所以我假设我只是做错了。实际上,在我的目标平台上(上面的和Linux2.6上的g++ 4.4.1 ),上面的unsigned long long等同于一个无符号64位整数,等同于相同内容的不同类型也是可以接受的(但我还没有找到)。
我可以使用ostringstream来格式化这个(或类似的)类型吗?如果没有,我可以不拖拽stdio和snprintf吗?更具哲学意义的是,键入是如何工作的,cout可以做到这一点,为什么该功能没有扩展到字符串流?
发布于 2011-08-29 14:23:22
这是因为
ostringstream o(); 没有声明一个变量,而是一个返回流的函数。
试试这个吧
ostringstream o; 另请参阅
https://stackoverflow.com/questions/7226769
复制相似问题