3

I'm getting this error message:

Operator<< in base class not visible in derived class

My Code:

class Dummy {};

class SomethingElse {};

class XBase
{
public:
    void noop() {};
    void operator <<(Dummy d) {};
};

class A : public XBase
{
public:
    void operator << (SomethingElse s) {}  // When commenting out statement: No compile error!
};

int main()
{
    Dummy dummy;
    SomethingElse somethingelse;
    XBase X;
    A a;

    X << dummy;
    a << dummy;         // why is Xbase::operator<< invisible, it's a different type
    a << somethingelse;
}

Compiler error messages:

error C2679 : binary '<<' : no operator found which takes a right - hand operand of type 'Dummy' (or there is no acceptable conversion)

message : could be 'void A::operator <<(SomethingElse)'
message : while trying to match the argument list '(A, Dummy)'

void operator <<(Dummy d) {}; in the base class "Xbase" becomes invisible by declaring any void operator <<(SomethingElse d) {}; in a derived class.

Why is this?

Tested with Visual Studio 19 C++ Compiler and GCC 7 under Linux (cross compiled with VS Studio 19)

Community
  • 1
  • 1
Raymund
  • 31
  • 3
  • 1
    (Great first question with a perfect [MCVE] and explanation in comments by the way!) – scohe001 Jan 31 '20 at 17:02
  • Why can't the compiler decide on the different parameter type like by function overloading? Is this the point where "name hiding" and "polymorphism" byte each other over the boundaries of base and derived classes? – Raymund Jan 31 '20 at 17:26
  • If you're interested, you can read more about why this happens here: https://www.programmerinterview.com/c-cplusplus/c-name-hiding/ – scohe001 Jan 31 '20 at 17:28
  • So the elegant (?) solution is to add " using XBase::operator<<;" to class A. – Raymund Jan 31 '20 at 17:54
  • That is correct. – scohe001 Jan 31 '20 at 17:55

0 Answers0