how to overload operator == outside template class using friend function?

sydridgm

I'm trying to write a template class which overloads operator==. I know how to get it inside the class:

    template <typename T>
    class Point
    {
    private:
        T x;
    public:
        Point(T X) : x(X) {}

        bool operator== (Point &cP)
        {
            return (cP.x == x);
        }
    };

But now I want to achieve this outside the template class. I've read this post: error when trying to overload << operator and using friend function and add template declaration in my code:

template <typename> class Point;
template <typename T> bool operator== (Point<T>, Point<T>);
template <class T>
class Point
{
private:
    T x;
public:
    Point(T X) : x(X) {}

    friend bool operator== (Point cP1, Point cP2);
};

template <class T>
bool operator== (Point<T> cP1, Point<T> cP2)
{
    return (cP1.x == cP2.x)
}

However I still get a error: unresolved external symbol "bool __cdecl operator==(class Point<int>,class Point<int>)" (??8@YA_NV?$Point@H@@0@Z) referenced in function _main

And when I take away friend from :

friend bool operator== (Point cP1, Point cP2);

and want it to be member function, there would be a another error:

too many parameters for this function

why?

mljli

@Kühl's answer is the most permissive approach to declare a templated friend function of a templated class. However, there is one unapparent side effect of this approach: All template instantiations of Point are friends with all template instantiations of operator==(). An alternative is to make only the instantiation with the same type of Point a friend. Which is done by adding a <T> in the friend declaration of operator==().

template <typename T> class Point;

template <typename S>
bool operator== (Point<S>, Point<S>);

template <typename T>
class Point {
    // ...
    friend bool operator==<T> (Point, Point);
};

References
http://web.mst.edu/~nmjxv3/articles/templates.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to overload operator == outside template class using friend function?

From Dev

error when trying to overload << operator and using friend function

From Dev

error when trying to overload << operator and using friend function

From Dev

operator = overload in template class

From Dev

Template class with operator overload

From Dev

C++: Making a template operator= overload a friend

From Dev

Overload operator<< for nested class template

From Dev

Overload operator<< for nested class template

From Dev

C++ - How to declare a function template friend for a class template

From Dev

how do I specialize a bound template friend function to a template class?

From Dev

Function declared outside class scope but not friend. How does this work?

From Dev

How can I overload a operator for a class with a generic template?

From Dev

How to define a friend function operator>> inside a local class?

From Dev

c++ - friend operator overloading with template class

From Dev

In-class friend operator doesn't seem to participate in overload resolution

From Dev

Template function overload for base class

From Dev

declare template friend function of template class

From Dev

Operator Overload for Template Class Auto Type Conversion

From Dev

How to overload operator<< for a class member?

From Dev

How to overload << operator in nested class

From Dev

How to overload operator << for derived classes using a shared base class?

From Dev

Passing a reference to template function call operator overload

From Dev

Passing a reference to template function call operator overload

From Dev

How to overload the friend extraction operator (>>) for templates in C++?

From Dev

Templated Class Friend Operator Member Function

From Dev

Defining a templated friend function inside a template class

From Dev

friend function for template class : C++

From Dev

Linking error with friend function in template class

From Dev

Friend template function in-class definition

Related Related

  1. 1

    how to overload operator == outside template class using friend function?

  2. 2

    error when trying to overload << operator and using friend function

  3. 3

    error when trying to overload << operator and using friend function

  4. 4

    operator = overload in template class

  5. 5

    Template class with operator overload

  6. 6

    C++: Making a template operator= overload a friend

  7. 7

    Overload operator<< for nested class template

  8. 8

    Overload operator<< for nested class template

  9. 9

    C++ - How to declare a function template friend for a class template

  10. 10

    how do I specialize a bound template friend function to a template class?

  11. 11

    Function declared outside class scope but not friend. How does this work?

  12. 12

    How can I overload a operator for a class with a generic template?

  13. 13

    How to define a friend function operator>> inside a local class?

  14. 14

    c++ - friend operator overloading with template class

  15. 15

    In-class friend operator doesn't seem to participate in overload resolution

  16. 16

    Template function overload for base class

  17. 17

    declare template friend function of template class

  18. 18

    Operator Overload for Template Class Auto Type Conversion

  19. 19

    How to overload operator<< for a class member?

  20. 20

    How to overload << operator in nested class

  21. 21

    How to overload operator << for derived classes using a shared base class?

  22. 22

    Passing a reference to template function call operator overload

  23. 23

    Passing a reference to template function call operator overload

  24. 24

    How to overload the friend extraction operator (>>) for templates in C++?

  25. 25

    Templated Class Friend Operator Member Function

  26. 26

    Defining a templated friend function inside a template class

  27. 27

    friend function for template class : C++

  28. 28

    Linking error with friend function in template class

  29. 29

    Friend template function in-class definition

HotTag

Archive