1
votes

I am sorry that I duplicate this question, but I don't have the reputation required to comment there and the answers there are not convincing for me.

#include<iostream>

class my_ostream : public std::ostream
{
    public:
    std::string prefix;

    my_ostream():prefix("*"){}

    my_ostream& operator<<(const std::string &s){
        std::cout << this->prefix << s;
        return *this;
    }
};

int main(){
  my_ostream s;
  std::string str("text");
  s << str << std::endl;
}

Here I get:

no match for ‘operator<<’ in ‘s.my_ostream::operator<<(((const std::string&)((const std::string*)(& str)))) << std::endl’

and I don't understand why. If it works for ostream, it should work for my_ostream. This program works:

#include <iostream>
using namespace std;

class a{};
class b:public a{};
class c:public b{};

void f(a){cout << 'a' << endl;}
void f(b){cout << 'b' << endl;}
void f(b, a){cout << "b, a" << endl;}
void f(c){cout << 'c' << endl;}
void f(c, int){cout << "c, int" << endl;}

void f(a*){cout << "pa" << endl;}
void f(b*){cout << "pb" << endl;}
void f(b*, a*){cout << "pb, pa" << endl;}
void f(c*){cout << "pc" << endl;}
void f(c*, int){cout << "pc, int" << endl;}

int main(){
  a ao; b bo; c co;
  f(ao); f(bo); f(co);
  f(co, ao);
  a *pa=new(a); b *pb=new(b); c *pc=new(c);
  f(pa); f(pb); f(pc);
  f(pc, pa);
  return 0;}

It outputs:

a
b
c
b, a
pa
pb
pc
pb, pa

So simple overloading does not explain this error. Also, I do not introduce templates here, so undetermined template type parameters should not play a role. Reading the iostream code proves to be very difficult, so I appreciate any insight.

2
It turns out that std::ostream s; does not work, either, so this is quite complicated and difficult to understand. - Doru Georgescu
The internet says that cout is of type ostream, though. Anybody can untie this node? :) - Doru Georgescu

2 Answers

4
votes

Simple overloading does explain this error. In fact, std::cout just complicates the issue. The following also doesn’t work:

int main(){
  my_ostream s;
  s << 1;
}

The issue is that your operator << overload in effect hides all the overloads that are defined for the base class.

Roughly speaking, C++ does overload resolution after scope resolution. So C++ first checks if there’s an operator << defined in the scope of your class. There is! So it stops searching for more general functions right there and only considers the functions already found for overload resolution. Alas, there’s only a single overload, for std::string so the call fails.

This can be fixed simply by defining operator << not as a member function but a free function:

my_ostream& operator<<(my_ostream& out, const std::string &s) {
    std::cout << out.prefix << s;
    return out;
}

… but of course this only fixes some of your problems because your class definition is simply semantically wrong; you cannot subclass the IO streams like this. Here my knowledge fails but I think in order to do what you want you should override the stream buffer’s uflow function.

1
votes

Another way to fix this is declared as friend the operator<< overloading.

class my_ostream : public std::ostream
{
   public:
     std::string prefix;

   my_ostream():prefix("*"){}

   template <class T>
     friend my_ostream& operator<<(my_ostream& my_os, const T& s){
        std::cout << my_os.prefix << s;
     return my_os;
   }
};