tags:

views:

450

answers:

4

Here is some code that used to work with my code, but is having a problem now:

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>

using namespace std;    
int main()
{
 stringstream out;
 out << 100;
 cout << out.str();
}

I get just blank output. I just changed to snow leopard with Xcode 3.2.

+1  A: 

it works for me. if there's a problem, it should be your gcc's.

btw, maybe you have to add fflush(stdout); after the cout << sometime the problem is stdout buffer

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>

using namespace std;
int main()
{
 stringstream out;
 out << 100;
 cout << out.str();
 fflush(stdout);
}
Fu4ny
This did not fix the problem, either. I went to Project Settings in Xcode and changed the base SDK for all configurations from OS X 10.6 -> 10.5 and this fixed the problem. Any ideas why?
Patrick Hogan
@Fu4ny: The `std::flush` isn't really necessary in this case because the stream is flushed anyway when the program exits. (The only problem might be that Patrick has a breakpoint on the closing `}` and wonders why his output doesn't appear. But that's very unlikely.)
sbi
I don't really know why, but I just add flush to make sure that the stream is flushed.Should be some problem with gcc or stdout
Fu4ny
A: 

Another idea is that you have a .o file left over from before you upgraded that's somehow messing things up. Mixing .o files from two different versions of the C++ compiler can cause all kinds of strange problems. I also do not discount the header file issue as well, though sstream should be including string.

Omnifarious
A: 

Shouldn't you add the end of string before converting to string?

cout << out.str() << sdt::ends;
+2  A: 

Get this exact same issue under the same conditions Snow Leopard 64-Bit XCode 3.2 Base SDK 10.6 and the switch to Base SDK 10.5 resolves it.

Apparently it's a SDK 10.6 issue.

and the correct workaround is to remove the preprocessor macros:

  • _GLIBCXX_DEBUG=1
  • _GLIBCXX_DEBUG_PEDANTIC=1

From the preprocessor settings (or else fall back to SDK 10.5 as above).

Apple Discussion Link

Bee