Template specialization with friend operators and direct implementation

Discussion in 'C++' started by Preben, May 3, 2011.

  1. Preben

    Preben Guest

    I've encountered a little problem:


    when defining these operators

    ----------

    friend bool operator!= <>(const A& p, const V& q) {
    return !(p==q);
    }

    friend bool operator< <>(const A& p, const B& q);

    ----------


    I get an error on the first declaration:
    defining explicit specialization 'operator!=<>' in friend declaration

    The second declaration works fine


    However, when removing the "<>", a warning for the second friend
    operator definition appear.

    ----------

    friend bool operator!= (const A& p, const V& q) {
    return !(p==q);
    }

    friend bool operator< (const A& p, const B& q);

    ----------

    and the warning is
    friend declaration bool operator< (const A&, const B&) declares a
    non-template function


    What is the exact difference, and how do I determine the correct use of
    "<>" in the second case?



    Best regards
    Preben
     
    Preben, May 3, 2011
    #1
    1. Advertising

  2. Preben

    Preben Guest

    > when defining these operators
    >
    > ----------
    >
    > friend bool operator!= <>(const A& p, const V& q) {
    > return !(p==q);
    > }
    >
    > friend bool operator< <>(const A& p, const B& q);
    >
    > ----------


    Minor error here: A, V, B should all have been A


    > ----------
    >
    > friend bool operator!= (const A& p, const V& q) {
    > return !(p==q);
    > }
    >
    > friend bool operator< (const A& p, const B& q);
    >
    > ----------


    And the same here


    > What is the exact difference, and how do I determine the correct use of
    > "<>" in the second case?



    And the implementation of the second operator is given below:
    -----------
    template<class T>
    inline bool operator<(const A& p, const A& q)
    {
    return (p.member < q.member);
    }
    -----------
     
    Preben, May 3, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruben Campos
    Replies:
    5
    Views:
    569
    Ruben Campos
    Nov 30, 2004
  2. John
    Replies:
    2
    Views:
    415
  3. Replies:
    4
    Views:
    504
  4. Joseph Turian
    Replies:
    2
    Views:
    491
  5. Andrey Dj
    Replies:
    3
    Views:
    803
    Andrey Dj
    Apr 5, 2012
Loading...

Share This Page