no match for ‘operator<<’ for std::endl after overload

早过忘川 提交于 2019-12-11 10:38:53

问题


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.


回答1:


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.




回答2:


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;
   }
};


来源:https://stackoverflow.com/questions/13631124/no-match-for-operator-for-stdendl-after-overload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!