Derived class function parameter as base class reference is causing C2678

Mossid

I made 'Derived' class which is deriving 'Base' class. It is using CRTP. Base class contains one unary and one binary operator. Derived class is implementing those virtual operator functions.

template <typename T> class Base
{
public:
    virtual bool operator==(T operand) = 0;
    virtual bool operator!() = 0;
};

class Derived : public Base<Derived>
{
public:
    virtual bool operator==(Derived operand){ return true; }
    virtual bool operator!(){ return false; }
};

Template function notf and equalf are made for testing Derived class's member operators. Function notf is taking one Base by reference, and calls its ! operator. Function equalf does similar thing.

template <typename T> bool notf(Base<T>& x)
{
    return !x;
}

template <typename T> bool equalf(Base<T>& x, Base<T>& y)
{
    return x == y;
}

And the main function calls those template functions.

int main()
{
    Derived x, y;
    cout << notf(x);
    cout << equalf(x, y);
    return 0;
}

And C2678 error is generated on equalf function. The compiler says, error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Base<Derived>' (or there is no acceptable conversion). But I don't know what is problem because notf function is working well. When the code compiled except equalf function, it works well.

When I made equalf function to show the type of parameters, it shows "class Derived" and "class Derived". If it's true, then why the error message says left-hand operand of type 'Base<Derived>'?

Cheers and hth. - Alf

Base<T>::operator==(T operand) can't take a Base<T> argument (since there's no conversion defined).

It's difficult to suggest a fix, because the code points in many possible design directions.

However, anyway, the idea of virtual comparison operator, or virtual assignment, is generally ungood, because it moves type checking to run time, so that you need much more testing, and more intricate code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing derived class by reference to function taking base class as parameter

From Dev

Calling function overload for a derived class parameter with a base class parameter

From Dev

Storing an object of a derived class in a reference to the base class

From Dev

Casting base class pointer to derived class (reference)

From Dev

C++ Passing a derived class as a base class template parameter

From Dev

Base Class Function Not Calling Derived Class Function

From Dev

Passing derived class to base function

From Dev

Passing derived class to base function

From Dev

Can derived object cast to base reference in c++ template class?

From Dev

Moq a base class function from a derived class

From Dev

Printing from a derived class with a base class function

From Dev

How to pass a derived class to a base class function and return the derived class c#

From Dev

Copy constructor with a parameter as a reference to a derived class

From Dev

Check template parameter of Base class in Derived

From Dev

Static Cast from ( Base Reference to Base Object ) to ( Derived Class Reference)

From Dev

Base class template member function shadowed in Derived class, albeit different parameter list

From Dev

Cannot convert parameter 1 from derived pointer to base class pointer reference

From Dev

C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

From Dev

C++ Calling overwritten function in derived from base class

From Dev

C++ call derived function from base class

From Dev

Detect assignment of base class to reference pointing at derived class

From Dev

Why would one create a Base Class object with reference to the Derived Class

From Dev

is there any way to prevent the derived class from initializing a base class reference

From Dev

C++ -- Assigning a derived class to a base class

From Dev

Upcasting derived class to base class in C#

From Dev

Override function parameter type with type of derived class

From Dev

Call base() from derived class to execute base class function?

From Dev

Calling a derived function from an array of the base class

From Dev

Use derived class type as parameter of generic action of base class

Related Related

  1. 1

    Passing derived class by reference to function taking base class as parameter

  2. 2

    Calling function overload for a derived class parameter with a base class parameter

  3. 3

    Storing an object of a derived class in a reference to the base class

  4. 4

    Casting base class pointer to derived class (reference)

  5. 5

    C++ Passing a derived class as a base class template parameter

  6. 6

    Base Class Function Not Calling Derived Class Function

  7. 7

    Passing derived class to base function

  8. 8

    Passing derived class to base function

  9. 9

    Can derived object cast to base reference in c++ template class?

  10. 10

    Moq a base class function from a derived class

  11. 11

    Printing from a derived class with a base class function

  12. 12

    How to pass a derived class to a base class function and return the derived class c#

  13. 13

    Copy constructor with a parameter as a reference to a derived class

  14. 14

    Check template parameter of Base class in Derived

  15. 15

    Static Cast from ( Base Reference to Base Object ) to ( Derived Class Reference)

  16. 16

    Base class template member function shadowed in Derived class, albeit different parameter list

  17. 17

    Cannot convert parameter 1 from derived pointer to base class pointer reference

  18. 18

    C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

  19. 19

    C++ Calling overwritten function in derived from base class

  20. 20

    C++ call derived function from base class

  21. 21

    Detect assignment of base class to reference pointing at derived class

  22. 22

    Why would one create a Base Class object with reference to the Derived Class

  23. 23

    is there any way to prevent the derived class from initializing a base class reference

  24. 24

    C++ -- Assigning a derived class to a base class

  25. 25

    Upcasting derived class to base class in C#

  26. 26

    Override function parameter type with type of derived class

  27. 27

    Call base() from derived class to execute base class function?

  28. 28

    Calling a derived function from an array of the base class

  29. 29

    Use derived class type as parameter of generic action of base class

HotTag

Archive