🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.lang.c++
3 messages
3 total messages Started by seedy Fri, 05 Apr 1996 00:29
Help needed in C++ Function templates
#1
Author: seedy
Date: Fri, 05 Apr 1996 00:29
58 lines
1889 bytes
When attempted to compile the following code using GNU's g++ (ver. 2.7.1)
on HP-UX, I got the following error :

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.

Could any one explain why this error happens.  Please send 
your response via mail to 'seedy@trigent.com' or post it here.

Cheers,

Praveen.
Re: Help needed in C++ Function templates
#3
Author: Peter Rye
Date: Thu, 04 Apr 1996 18:06
53 lines
2432 bytes
>>>>> "seedy" == seedy  <seedy@ultranet.com> writes:

    seedy> When attempted to compile the following code using GNU's
    seedy> g++ (ver. 2.7.1) on HP-UX, I got the following error :

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


    seedy> #include <iostream.h>

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

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

    seedy> main() { 
    seedy>    abc a1(5), a2(5);
    seedy> cout << "Return value is = " << equal(a1, a2) << endl; }

    seedy> Could any one explain why this error happens.  Please send
    seedy> your response via mail to 'seedy@trigent.com' or post it
    seedy> here.

My compiler gives the following error.

ub239:~$ g++ -Wall -o check check.cc
check.cc: In function `int equal(const class abc &, const class abc &)':
check.cc:14: non-const member function `abc::operator ==(abc)'
check.cc:24: called for const object at this point in file

Making int operator == (abc) a constant member function -> compiles and 
runs without error.
ie:
 inline int operator ==(abc x) const { 
               return ((a==x.a) && (b==x.b)); 
        }
--
Peter Rye   prye@cyllene.uwa.edu.au,  prye@ichr.uwa.edu.au
Respiratory Research Fellow, Princess Margaret Hospital for Children
Perth, Western Australia    Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
** Smoking areas in restaurants are like peeing areas in swimming pools. **
Re: Help needed in C++ Function templates
#2
Author: Amir
Date: Thu, 04 Apr 1996 18:23
75 lines
2558 bytes
seedy@ultranet.com wrote:
> 
> When attempted to compile the following code using GNU's g++ (ver. 2.7.1)
> on HP-UX, I got the following error :
> 
> 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.
> 
> Could any one explain why this error happens.  Please send
> your response via mail to 'seedy@trigent.com' or post it here.
> 
> Cheers,
> 
> Praveen.

You are calling with const parameters a method that
it's parameters are not const.
change your operator== to:

       inline int operator ==(const abc & x) const
           {
              return ((a==x.a) && (b==x.b));
           }

Amir


--
--        o o
-----ooo---O---ooo-----------------------------------------
--  Amir Deutel
--
--  Home: Amir@bayarea.net    http://www.bayarea.net/~amir/
--  Work: Amir_Deutel@powertalk.apple.com
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