Article View: gnu.g++.help
Article #938Help needed on function templates
From: seedy
Date: Fri, 05 Apr 1996 02:17
Date: Fri, 05 Apr 1996 02:17
57 lines
1857 bytes
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.
Technical Details
Message-ID:
<NEWTNews.828674472.23501.seedy@trigent.trigent.com>Path:
arc1-ca.pugleaf.net!news.ultranet.com!usenetShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.ultranet.com!usenet From: seedy@ultranet.com Newsgroups: gnu.g++.help,comp.sys.lang.c++,comp.lang.c++ Subject: Help needed on function templates Date: Thu, 04 Apr 96 19:17:06 PDT Organization: UltraNet Communications, Inc. Message-ID: <NEWTNews.828674472.23501.seedy@trigent.trigent.com> NNTP-Posting-Host: trigent.ultranet.com Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage