Questions tagged [stream-operators]

Operators used for extracting or inserting into a stream.

In C++ operator<< is called the "Insertion Operator" and operator>> is called the "Extraction Operator"

These are commonly overloaded for individual classes for example:

struct foo{string bar;};

ostream& operator<<(ostream& os, const foo& bar){
    os << foo.bar;
    return os;
}

istream& operator>>(istream& is, const foo& bar){
    is >> foo.bar;
    return is;
}

foo can be used as follows:

foo bar;

cin >> bar;
cout << bar;
24 questions
18
votes
4 answers

calling operator<< in gdb

How do you call operator<<(std::ostream &os, const ClassX &x) from inside gdb ? In other words, how do you print an object in gdb ? call std::cout<
Ben
  • 6,534
  • 7
  • 35
  • 45
5
votes
4 answers

What's the right way to overload the stream operators << >> for my class?

I'm a bit confused about how to overload the stream operators for my class in C++, since it seems they are functions on the stream classes, not on my class. What's the normal way to do this? At the moment, for the "get from" operator, I have a…
ghallio
  • 223
  • 1
  • 3
  • 5
4
votes
2 answers

Trying to understand distinct() for streams in Java8

I was going through a book on Java8, where distinct was explained for stream. It is mentioned that the equality in order to produce distinct elements is determined by the implementation of hashCode() & equals() method. Therefore I wrote the below…
Chota Bheem
  • 1,026
  • 14
  • 27
3
votes
1 answer

How to define output stream operator for boost log and a custom type

I was able to define the output stream operator for a simple struct, however, not for std::array. The following code fails to compile. What is wrong and how can I fix it? #include #include #include #include…
3
votes
1 answer

Overload of operator<< with basic_ostream

Why the typical header of stream manipulation with a user-defined class C is typically like this: std::ostream& operator<<(std::ostream& os, const C& c); std::istream& operator>>(std::istream& is, C&); and not like this: template
3
votes
1 answer

Why Isn't to_string Templatized?

I thought that to_string was just templatized and used stringstream under the hood. Is that not the case? I want to be able to do this: class foo{}; ostream& operator<<(ostream& os, const foo& /*bar*/){ os << "foo"; return os; } int main()…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
2
votes
1 answer

Is there an compiler bug in vc++? (I am a beginner)

There are different effect between vc++(debug mode) and g++, with this code test.hh class Test { public: int a, b, c; Test(int a, int b, int c) : a{ a }, b{ b }, c{ c } { } Test& operator++(); Test …
Steve Wilson
  • 102
  • 4
2
votes
2 answers

operator << (stream output) for nullptr

Consider a piece of generic C++ code which outputs to a stream the values of its arguments in case they are not equal: #define LOG_IF_NE(a, b) if(a != b) { \ std::cerr << "Failed because (" << ##a << "=" << (a) << \ ") != (" << ##b <<…
Serge Rogatch
  • 11,119
  • 4
  • 58
  • 117
2
votes
2 answers

How to implement the extraction operator in a class?

I have a class that reads parts of a binary file into variables of different types. class Foo { public: size_t getSizeT(); float getFloat(); std::string getString(); private: std::ifstream stream; }; Now I'd like to implement the…
danijar
  • 27,743
  • 34
  • 143
  • 257
2
votes
4 answers

using nested-types of a template-class as template parameter

I want to implement a template function using nested-types of a template-class. I have just read here that it is better to implement operator << as non-member and non-friend function. Therefore I decided to move functions toStream() and…
oHo
  • 41,098
  • 25
  • 141
  • 183
2
votes
1 answer

ADL can't locate stream operator with appropriate qualifiers for a user defined type

I'm compiling an x64 service on Microsoft Windows 7 with Visual Studio 2010, using a Boost variant something like: namespace my_ns { typedef struct {} empty_t; typedef std::pair> string_t; typedef…
Matthew Reddington
  • 1,287
  • 1
  • 12
  • 23
1
vote
1 answer

Making multiple operator<<() definition for a single class

So basically this is what I want to make: void someMethod() { StreamClass streamClass{}; streamClass << "Something" << "\n"; //do something streamClass.doA << "Something" << "\n"; //do another thing streamClass.doB << "Something" <<…
1
vote
1 answer

Custom stream class used as temporary

I want to code another stream class. Done it before like this: class MyStream { // ... }; template MyStream& operator <<(MyStream& s, const T& t) noexcept { std::cout << t; return s; } void f() { MyStream s; s <<…
Silicomancer
  • 7,280
  • 6
  • 49
  • 101
1
vote
2 answers

Pass multiple arguments to stream operator

I have a class, let's call it Sample with variadic template arguments. This class contains a function run(Args... args). This class also implements a stream operator that calls this function. The class looks like this: template
Timo
  • 7,388
  • 2
  • 18
  • 44
1
vote
2 answers

problem with QDataStream & QDataStream::operator>> ( char *& s )

QFile msnLogFile(item->data(Qt::UserRole).toString()); QDataStream logDataStream; if(msnLogFile.exists()){ msnLogFile.open(QIODevice::ReadOnly); logDataStream.setDevice(&msnLogFile); QByteArray logBlock; …
yan bellavance
  • 4,344
  • 19
  • 56
  • 87
1
2