🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.lang.c++
Article #3

Re: Help needed in C++ Function templates

#3
From: 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. **
Technical Details
Message-ID: <PRYE.96Apr5020646@cyllene.uwa.edu.au>
Path: arc1-ca.pugleaf.net!ub239.dialup.uwa.edu.au!localhost!prye
References: <NEWTNews.828668110.2725.seedy@trigent.trigent.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!ub239.dialup.uwa.edu.au!localhost!prye
From: prye@cyllene.uwa.edu.au (Peter Rye)
Newsgroups: comp.sys.lang.c++,comp.lang.c++
Subject: Re: Help needed in C++ Function templates
Date: 04 Apr 1996 18:06:46 GMT
Organization: The University of Western Australia
Message-ID: <PRYE.96Apr5020646@cyllene.uwa.edu.au>
References: <NEWTNews.828668110.2725.seedy@trigent.trigent.com>
NNTP-Posting-Host: ub239.dialup.uwa.edu.au
In-reply-to: seedy@ultranet.com's message of Thu, 04 Apr 96 17:29:36 PDT
X-Mailer: GNU Emacs 19.28