Problem with member and non-member binary operator in template class

Discussion in 'C++' started by ghager, Jan 16, 2006.

  1. ghager

    ghager Guest

    Hi all,

    I must be blind or stupid. Please consider the following code:

    ----
    ....
    template <class T> class P;
    template <class T> P<T> operator*(T,const P<T>&);

    template <class T>
    class P{
    private:
    T d;
    public:
    P(int i=0);
    P<T> operator*(T);
    friend P<T> operator*<>(T,const P<T>&); // this is line 15
    };

    template <class T> P<T> operator*(T x,const P<T>& p)
    {
    return P<T>(x*p.d);
    }

    template <class T>
    P<T>::p(int i) : d(i) {}

    template <class T>
    P<T> P<T>::eek:perator*(T x)
    {
    return P<T>(x*d);
    }

    int main()
    {
    P<int> p(4),q,r;
    q=3*p;
    r=p*3;
    return 0;
    }
    ----

    Compiling this code, e.g. g++ 4.0 says:

    opp.cc:15: error: declaration of 'operator*' as non-function
    opp.cc:15: error: expected ';' before '<' token

    Intel 9.0 says:

    opp.cc(15): error: function "P<T>::eek:perator* [with T=int]" is not a
    template
    friend P<T> operator*<>(T,const P<T>&);


    Mysteriously, when I interchange the friend declaration in P
    with the declaration of the member operator*, everything
    is fine. What's going on?

    Thanks for any help,
    bye,
    Georg.
    ghager, Jan 16, 2006
    #1
    1. Advertising

  2. Re: Problem with member and non-member binary operator in templateclass

    I'm not an expert, but I think if operator is friend, it must not belong
    to P when you define it (it must be operator*, and not P<T>::eek:perator*)


    ghager wrote:
    > Hi all,
    >
    > I must be blind or stupid. Please consider the following code:
    >
    > ----
    > ...
    > template <class T> class P;
    > template <class T> P<T> operator*(T,const P<T>&);
    >
    > template <class T>
    > class P{
    > private:
    > T d;
    > public:
    > P(int i=0);
    > P<T> operator*(T);
    > friend P<T> operator*<>(T,const P<T>&); // this is line 15
    > };
    >
    > template <class T> P<T> operator*(T x,const P<T>& p)
    > {
    > return P<T>(x*p.d);
    > }
    >
    > template <class T>
    > P<T>::p(int i) : d(i) {}
    >
    > template <class T>
    > P<T> P<T>::eek:perator*(T x)
    > {
    > return P<T>(x*d);
    > }
    >
    > int main()
    > {
    > P<int> p(4),q,r;
    > q=3*p;
    > r=p*3;
    > return 0;
    > }
    > ----
    >
    > Compiling this code, e.g. g++ 4.0 says:
    >
    > opp.cc:15: error: declaration of 'operator*' as non-function
    > opp.cc:15: error: expected ';' before '<' token
    >
    > Intel 9.0 says:
    >
    > opp.cc(15): error: function "P<T>::eek:perator* [with T=int]" is not a
    > template
    > friend P<T> operator*<>(T,const P<T>&);
    >
    >
    > Mysteriously, when I interchange the friend declaration in P
    > with the declaration of the member operator*, everything
    > is fine. What's going on?
    >
    > Thanks for any help,
    > bye,
    > Georg.
    >
    Carlos Martinez Garcia, Jan 16, 2006
    #2
    1. Advertising

  3. ghager

    ghager Guest

    Carlos Martinez Garcia wrote:
    > I'm not an expert, but I think if operator is friend, it must not belong
    > to P when you define it (it must be operator*, and not P<T>::eek:perator*)


    In fact I have two operator* functions, one is a member with
    one argument and one is a non-member with two arguments (the
    original class type as second argument). So the compiler should know
    when to select which, but obviously things get mixed up.
    ghager, Jan 16, 2006
    #3
  4. Re: Problem with member and non-member binary operator in templateclass

    ghager wrote:
    > Carlos Martinez Garcia wrote:
    >
    >>I'm not an expert, but I think if operator is friend, it must not belong
    >> to P when you define it (it must be operator*, and not P<T>::eek:perator*)

    >
    >
    > In fact I have two operator* functions, one is a member with
    > one argument and one is a non-member with two arguments (the
    > original class type as second argument). So the compiler should know
    > when to select which, but obviously things get mixed up.
    >

    Yes. You're right. I haven't see the second (non-member operator*)
    I test it with g++ 3.3.6 and compiles ok

    I'm sorry
    Carlos Martinez Garcia, Jan 18, 2006
    #4
  5. ghager

    ghager Guest


    > Yes. You're right. I haven't see the second (non-member operator*)
    > I test it with g++ 3.3.6 and compiles ok


    Yes, older compilers like gcc 3.3.X and SUN WS6 accept this code.
    But all newer ones (PGI 6.0, Intel 9.1beta, gcc 4.0) generate
    an error.

    Another piece of information: If the class isn't a template, it works
    fine.

    Clueless :-(,
    Georg.
    ghager, Jan 18, 2006
    #5
  6. ghager wrote:
    > Hi all,
    >
    > I must be blind or stupid. Please consider the following code:
    >
    > template <class T> class P;
    > template <class T> P<T> operator*(T,const P<T>&);
    >
    > template <class T>
    > class P{
    > private:
    > T d;
    > public:
    > P(int i=0);
    > P<T> operator*(T);
    > friend P<T> operator*<>(T,const P<T>&); // this is line 15


    I think you mean:

    friend P<T> operator*(T, constP<T>&);

    > };
    >
    > template <class T> P<T> operator*(T x,const P<T>& p)
    > {
    > return P<T>(x*p.d);
    > }
    >
    > template <class T>
    > P<T>::p(int i) : d(i) {}
    >
    > template <class T>
    > P<T> P<T>::eek:perator*(T x)
    > {
    > return P<T>(x*d);
    > }
    >
    > int main()
    > {
    > P<int> p(4),q,r;
    > q=3*p;
    > r=p*3;
    > return 0;
    > }
    > ----


    Best regards,

    Tom
    Thomas Tutone, Jan 18, 2006
    #6
  7. ghager

    ghager Guest

    No, this is a friend function declaration in which the
    friend is an appropriately typed function which
    is not a template specialization (C++ standard
    ยง14.5.3).

    What I want to have is a friend that is an appropriately
    typed function template specialization for each specialization
    of the class template.

    Bye,
    Georg.
    ghager, Jan 19, 2006
    #7
    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. BigMan
    Replies:
    3
    Views:
    1,426
    BigMan
    Apr 22, 2005
  2. Replies:
    5
    Views:
    566
    dasjotre
    Nov 22, 2006
  3. nguillot
    Replies:
    5
    Views:
    527
  4. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    412
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page