ostream

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complaints, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++. Here i...

creating an ostream

I am trying to create a c++ ostream for educational reasons. My test will be creating an ostream that acts like a ofstream would except instead of writing to a file it would write to a deque or vector container. ...

how do i create an ostream/streambuf?

For educational purposes i want to create a ostream and stream buffer to do A) fix endians when doing << myVar; B) store in a deque container instead of using std:cout or writing to a file C) log extra data, such as how many times i did <<, how many times i did .write, the amount of bytes i written and how many times i flush(). But i do ...

Compiler not creating templated ostream << operator

I have a class, defined in a head as: template <typename T> class MyClass { template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p); public: ... } In an implementation file, I have: template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m) { outpu...

how do I print an unsigned char as hex in c++ using ostream?

I want to work with unsigned 8-bit variables in C++. Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for unsigned char, or so the debugger presents it. The problem is that if I print out the variables using ostream in C++ it treats it as char. ...

How to inherit from std::ostream?

I've been googling around and I just can't find a simple answer to this. And it should be simple, as the STL generally is. I want to define MyOStream which inherits publicly from std::ostream. Let's say I want to call foo() each time something is written into my stream. class MyOStream : public ostream { public: ... private: void ...

Have a C++ Class act like a custom ostream, sstream

I have a C++ class MyObject and I want to be able to feed this data like I would to a osstream (but unlike a direct sstream, have the incoming data be formatted a special way). I can't seem to figure out how to overload a operator for MyObject to eat input given to it. class MyObject { public: ostringstream s; FEEDME }; int m...

Is this the correct way to overload the left-stream operator? (C++)

This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are: error C2143: syntax error : missing ';' before '&' error C4430: missing type specifier error C2065: 'os' : undeclared identifier error C2065: 'obj' : undeclared identifier error C2275: 'hand' : illegal use of this type as an expres...

Function that prints something to std::ostream and returns std::ostream?

I want to write a function that outputs something to a ostream that's passed in, and return the stream, like this: std::ostream& MyPrint(int val, std::ostream* out) { *out << val; return *out; } int main(int argc, char** argv){ std::cout << "Value: " << MyPrint(12, &std::cout) << std::endl; return 0; } It would be conveni...

Printing an uninitialized bool using cout (C++)

I have a class with a bool data member that is not initialized by the constructor. If I do cout << x.myBoolDataMember; where x is an object of this class in which the bool has not been initialized, I sometimes get a random number rather than 0 or 1. (I'm using gcc.) Is this behavior compliant with the Standard? ...

C++ - Passing std::ostream to a function

Hi I thought of a small debug inline function in C++: void inline debug( int debug_level, ostream& out ) { if ( debug_level <= verbosity ) { out.flush(); } else { ostream tmp; tmp << out; } } This is an example of how I wanted to use it: _debug( 7, cout << "Something something" << someint << e...

Getting text data from C++ using JNI through std::ostream into Java

Hi everyone, I have a class in C++ which takes an std::ostream as an argument in order to continuously output text (trace information). I need to get this text over to the Java side as efficiently as possible. What's the best way to do this? I was thinking of using a direct buffer, but another method would be to take all the function cal...

Overloading << operator and recursion

Hi, I tried the following code: #include <iostream> using std::cout; using std::ostream; class X { public: friend ostream& operator<<(ostream &os, const X& obj) { cout << "hehe"; // comment this and infinite loop is gone return (os << obj); } }; int main() { X x; cout << x; return 0; }...

Deriving streambuf or basic_ostringstream?

Hi, I want to derive a stringstream so that I can use the operator<< to construct a message which will then be thrown. The API would look like: error("some text") << " more text " << 42 << std::endl; This should do a throw "some text more text 42" So what I did is make an errorbuf (inheriting from streambuf) which overloads the 'ov...

Setting minimum number of decimal places for std::ostream precision

Hi, Is there a way to set the "minimum" number of decimal places that a std::ostream will output? For example, say I have two unknown double variables that I want to print (values added here for the sake of illustration): double a = 0; double b = 0.123456789; I can set my maximum decimal precision so that I output b exactly std::c...

The result of int c=0; cout<<c++<<c;

I think it should be 01 but someone says its "undefined", any reason for that? ...

How can i convert from wostream to ostream

i am using a function that receives ostream but i have wostream is there a way to convert one to the other? in particular i want to use boost::write_graphviz which takes ostream but i currently in << operator for wostream. ...

Need to make context available to C++ ostream insertion operators

For an API that I am working on, I want to allow the user to insert custom objects into an ostream, but these objects have no meaning on their own, and are too memory constrained to include an additional pointer or reference for context. (Think tens of millions of 16-/32-/48-bit objects in an embedded system with limited memory.) Suppo...

Writting Unicode Characters to an OStream

I'm working with unicode/wide characters and I'm trying to create a toString method (Java ::toString equiv). Will ostream handle wide characters, if so is there a way to warn the consumer of the stream that it is unicode coming out of it? ...

Check if ostream object is cout or ofstream, c++

Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that ...