Thread: Stuck with friend functions and class template

  1. #1
    Registered User srikrish85's Avatar
    Join Date
    Jun 2015
    Posts
    6

    Stuck with friend functions and class template

    Hi,
    How do you do? I need to declare a friend function to my class... okay Let me post my code

    Code:
    template <typename T,int N> class MySequence;
    
    template <typename T,int N>
    bool operator==(const MySequence<T,N>&, const MySequence<T,N>&);
    
    template <typename T,int N> class MySequence
    
    {
    friend bool operator== <T> (const MySequence<T,N>&,
    const MySequence<T,N>&);
    // my members and other methods
    }; bool operator== (const MySequence<T,N>& lhs,
    const MySequence<T,N>& rhs)
    {
    return lhs.value==rhs.value;
    }
    Well the above code works fine. Now the thing is when I compare...

    Code:
    MySequence<int,10> ints;
    MySequence<double,10> ds;
    when write
    Code:
    if(ints==ds)
    {
    //some stuff
    }
    the compiler throws error because both are different objects.
    But the comparison as shown below works fine
    Code:
    MySequence<int,10> ten_ints;
    MySequence<int,2>   two_ints;
    
    if(ten_ints==two_ints)
    {
    //some stuff
    } else {
    //do something
    }
    So I commented my friend function and incorporated operator overloading in the class itself
    Code:
    template <typename T,int N> class MySequence
    {
    /*friend bool operator== <T> (const MySequence<T,N>&,
    const MySequence<T,N>&);*/
    public:
    //some methods
    template <typename T2,int N2>
    bool operator==(const MySequence<T,N>& lhs,
    const MySequence<T2,N2>& rhs)
    {
    return lhs.value==rhs.value;
    } private: //members
    };
    well the above works fine now i.e. I am able to compare int instance with double instance. But Is there a way to do it using friend functions?

    Thanks in advance
    -srikrish

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that in your member version of the overload, you have both T and T2, N and N2 template parameters. This suggests that you can do something similiar for your non-member version:
    Code:
    template <typename T, int N, typename T2, int N2>
    bool operator==(const MySequence<T, N>& lhs, const MySequence<T2, N2>& rhs)
    {
        return lhs.value == rhs.value;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User srikrish85's Avatar
    Join Date
    Jun 2015
    Posts
    6
    Hi,
    Thanks for the reply. I already did what you said,
    the thing is how am I going to tell my class that this function is the friend.
    I tried to put the following inside the class
    Code:
    friend bool operator== <T,E,N,M>(const MySequence<T,N>&,const MySequence<E,M>&);
    and my compiler yells the error
    Code:
    error: ‘E’ was not declared in this scope
       friend bool operator== <T,E,N,M>(const MySequence<T,N>&,const MySequence<E,M>&);
    I also tried placing
    Code:
    friend bool operator== <T,E>(const MySequence<T,N>&,const MySequence<E,M>&);
    but never worked. So I resorted to overloading operator inside the class itself (as a method) and it worked.

    Sorry for asking so many questions, just stared learning programming a few weeks back.

    Thanks & Regards,
    srikrish

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could try:
    Code:
    template <typename T, int N>
    class MySequence
    {
        // ...
    
        template <typename T1, int N1, typename T2, int N2>
        friend bool operator==(const MySequence<T1, N1>& lhs, const MySequence<T2, N2>& rhs);
    
        // ...
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User srikrish85's Avatar
    Join Date
    Jun 2015
    Posts
    6
    Hi,
    Sorry for replying late. Thanks for the response.
    -Sridhar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. friend function template class declaration error
    By -EquinoX- in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2009, 01:00 PM
  2. template friend operator functions
    By KIBO in forum C++ Programming
    Replies: 12
    Last Post: 09-15-2009, 07:11 AM
  3. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. Friend template functions
    By lyx in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 01:11 PM