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.