🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by seedy Fri, 05 Apr 1996 02:17
Help needed on function templates
#938
Author: seedy
Date: Fri, 05 Apr 1996 02:17
57 lines
1857 bytes
When attempted to compile the following code I got the following error :
I use g++ (2.7.1) on HP-UX 10.x.

news.cc: In function `int equal(const class abc &, const class abc &)':
news.cc:23: no match for `operator ==(class abc, class abc)'

#include <iostream.h>

class abc {
    public : 
       abc( int x )
	  {
	  a = x;
	  b = 0;
	  }
       int a;
       int b;
       inline int operator ==(abc x)
           {
              return ((a==x.a) && (b==x.b));
           }
    };



template < class Element>
inline int equal (Element const& e1, Element const& e2) 
{
  return e1 == e2;

}

main()
{
abc a1(5), a2(5);

cout << "Return value is = " << equal(a1, a2) << endl;
}


Even though the 'operator ==' is overloaded in class abc, compiler is
reporting match not found. By making the overloaded function 'operator =='
as friend to 'class abc' I could resolve the problem, because in this case,
this overloaded function becomes outsider function.

As far as I know, template instantiation for function 'equal will be done
for 'class abc'(because the passed parameters are of type class abc), 
and so the corresponding member function 'operator ==' can be used for 
template variables e1 and e2.

Any explanations?  Please forward your response to 'seedy@trigent.com'
or post it here.

Regards,

Praveen.
Re: Help needed on function templates
#939
Author: Dietmar Kuehl
Date: Thu, 04 Apr 1996 15:40
78 lines
2997 bytes
Hi,

seedy@ultranet.com wrote:
: When attempted to compile the following code I got the following error :
: I use g++ (2.7.1) on HP-UX 10.x.

: news.cc: In function `int equal(const class abc &, const class abc &)':
: news.cc:23: no match for `operator ==(class abc, class abc)'

There is indeed no 'operator==()' defined in your class which takes a
const object as first argument and there is no built-in conversion from
a const object to a non-const one (sure: this would mean to create a
non-const unnamed temporary but all unnamed temporaries are const).
Thus, all you need to do, is to declare your 'operator==()' as it
should be declared: as 'const'.

: #include <iostream.h>

: class abc {
:     public : 
:        abc( int x )
: 	  {
: 	  a = x;
: 	  b = 0;
: 	  }
:        int a;
:        int b;
:        inline int operator ==(abc x)
         inline int operator == (abc x) const

Actually, I think this operator should be declared as
	 inline bool operator == (abc const &x) const

The implementation can stay as is...
:            {
:               return ((a==x.a) && (b==x.b));
:            }
:     };



: template < class Element>
: inline int equal (Element const& e1, Element const& e2) 
: {
:   return e1 == e2;

: }

: main()
: {
: abc a1(5), a2(5);

: cout << "Return value is = " << equal(a1, a2) << endl;
: }


: Even though the 'operator ==' is overloaded in class abc, compiler is
: reporting match not found. By making the overloaded function 'operator =='
: as friend to 'class abc' I could resolve the problem, because in this case,
: this overloaded function becomes outsider function.

You probably declared your "outsider" 'operator==' like this:
  int operator== (abc a, abc b) { /*...*/ }

In this case, a (non-const) copy is made of the 'const' arguments
passed to it in the 'equal()' function. This is the reason why it
worked then.

: As far as I know, template instantiation for function 'equal will be done
: for 'class abc'(because the passed parameters are of type class abc), 
: and so the corresponding member function 'operator ==' can be used for 
: template variables e1 and e2.

You are right: for non-const variables it should work...
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl/
I am a realistic optimist - that's why I appear to be slightly pessimistic
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads