ostringstream operator<< for long?
$ 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 philo开发者_如何学Pythonsophically, how does the typing work out that cout can do it, and why wasn't that functionality extended to the string stream stuff?
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?
精彩评论