PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Template operators
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Template operators

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 16h19   #1
D-Dog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Template operators

Hi,
I'm having some trouble overloading the '==' operator. I've snipped
irrelevant parts to demonstrate the problem. Perhaps someone can see
what I'm doing wrong.

I have a template class:

template<class T> class testFile {

private:
T data;
public:

bool operator==(testFile &val) {
return(data == val.data);
}

--------------------------------------------------------

The above works fine for any instance of testFile<{insert data type}>

However, when I do the following

class testFile2 {
private:

std::vector< testFile<{some-data-type}> > testThis;

public:

bool operator==(testFile2 &this_test) {
return(testThis == this_test.testThis);
}
}

I get the following compile error:

/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_algobase.h: In function `bool std::equal(_InputIterator1,
_InputIterator1, _InputIterator2) [with _InputIterator1 =
__gnu_cxx::__normal_iterator<const testFile<float>*,
std::vector<testFile<float>, std::allocator<testFile<float> > > >,
_InputIterator2 = __gnu_cxx::__normal_iterator<const testFile<float>*,
std::vector<testFile<float>, std::allocator<testFile<float> > > >]':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_vector.h:878: instantiated from `bool std:perator==(const
std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)[with _Tp =
testFile<float>, _Alloc = std::allocator<testFile<float> >]'
.../../src/include/../testFile2.h:24: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_algobase.h:691: error:no match for 'operator==' in
'(&__first1)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>:perator* [with _Iterator = const testFile<float>*,
_Container = std::vector<testFile<float>,
std::allocator<testFile<float> > >]() == (&__first2)-
>__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with

_Iterator = const testFile<float>*, _Container =
std::vector<testFile<float>, std::allocator<testFile<float> > >]()'
/../../src/include/../testFile.h:24: note: candidates are: bool
testFile<T>:perator==(testFile<T>&) [with T = float]

Apparently the vector is having some trouble in its operator locating
the proper template operator. Any idea how to handle this?

Thanks,

Dennis

  Réponse avec citation
Vieux 07/04/2008, 16h23   #2
Marcel Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template operators

D-Dog wrote:
> bool operator==(testFile &val) {
> return(data == val.data);
> }


The signature is wrong. Use:

bool operator==(const testFile &val) const {


> However, when I do the following
>
> class testFile2 {
> private:
>
> std::vector< testFile<{some-data-type}> > testThis;
>
> public:
>
> bool operator==(testFile2 &this_test) {
> return(testThis == this_test.testThis);
> }


Same here, but vector depend on correct signature.


Marcel
  Réponse avec citation
Vieux 07/04/2008, 16h34   #3
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template operators

D-Dog wrote:

> Hi,
> I'm having some trouble overloading the '==' operator. I've snipped
> irrelevant parts to demonstrate the problem. Perhaps someone can see
> what I'm doing wrong.
>
> I have a template class:
>
> template<class T> class testFile {
>
> private:
> T data;
> public:
>
> bool operator==(testFile &val) {
> return(data == val.data);
> }


So this operator claims to modify both the left hand and the right hand
operand.


> bits/stl_algobase.h:691: error:no match for 'operator==' in
> '(&__first1)->__gnu_cxx::__normal_iterator<_Iterator,
> _Container>:perator* [with _Iterator = const testFile<float>*,
> _Container = std::vector<testFile<float>,
> std::allocator<testFile<float> > >]() == (&__first2)-
>>__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with

> _Iterator = const testFile<float>*, _Container =
> std::vector<testFile<float>, std::allocator<testFile<float> > >]()'
> /../../src/include/../testFile.h:24: note: candidates are: bool
> testFile<T>:perator==(testFile<T>&) [with T = float]
>
> Apparently the vector is having some trouble in its operator locating
> the proper template operator.


Because there isn't one. It's searching for an operator that does not modify
its operands.

> Any idea how to handle this?


Make your operator:

bool operator==(const testFile &val) const {
return(data == val.data);
}

And read up on const correct programming.


  Réponse avec citation
Vieux 07/04/2008, 16h38   #4
D-Dog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template operators

On Apr 7, 11:23 am, Marcel Müller <news.5.ma...@spamgourmet.org>
wrote:
> D-Dog wrote:
> > bool operator==(testFile &val) {
> > return(data == val.data);

>
> > }

>
> The signature is wrong. Use:
>
> bool operator==(const testFile &val) const {
>
> > However, when I do the following

>
> > class testFile2 {
> > private:

>
> > std::vector< testFile<{some-data-type}> > testThis;

>
> > public:

>
> > bool operator==(testFile2 &this_test) {
> > return(testThis == this_test.testThis);
> > }

>
> Same here, but vector depend on correct signature.
>
> Marcel


Won't using a const lead to a "discards qualifier" error when trying
to compile?
  Réponse avec citation
Vieux 07/04/2008, 16h41   #5
D-Dog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template operators

On Apr 7, 11:34 am, Rolf Magnus <ramag...@t-online.de> wrote:
> D-Dog wrote:
> > Hi,
> > I'm having some trouble overloading the '==' operator. I've snipped
> > irrelevant parts to demonstrate the problem. Perhaps someone can see
> > what I'm doing wrong.

>
> > I have a template class:

>
> > template<class T> class testFile {

>
> > private:
> > T data;
> > public:

>
> > bool operator==(testFile &val) {
> > return(data == val.data);
> > }

>
> So this operator claims to modify both the left hand and the right hand
> operand.
>
> > bits/stl_algobase.h:691: error:no match for 'operator==' in
> > '(&__first1)->__gnu_cxx::__normal_iterator<_Iterator,
> > _Container>:perator* [with _Iterator = const testFile<float>*,
> > _Container = std::vector<testFile<float>,
> > std::allocator<testFile<float> > >]() == (&__first2)-
> >>__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with

> > _Iterator = const testFile<float>*, _Container =
> > std::vector<testFile<float>, std::allocator<testFile<float> > >]()'
> > /../../src/include/../testFile.h:24: note: candidates are: bool
> > testFile<T>:perator==(testFile<T>&) [with T = float]

>
> > Apparently the vector is having some trouble in its operator locating
> > the proper template operator.

>
> Because there isn't one. It's searching for an operator that does not modify
> its operands.
>
> > Any idea how to handle this?

>
> Make your operator:
>
> bool operator==(const testFile &val) const {
> return(data == val.data);
> }
>
> And read up on const correct programming.


The second const was the key. Thanks for your .
  Réponse avec citation
Vieux 07/04/2008, 17h14   #6
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Template operators

D-Dog wrote:
> On Apr 7, 11:23 am, Marcel Müller <news.5.ma...@spamgourmet.org>
> wrote:
>> D-Dog wrote:
>>> bool operator==(testFile &val) {
>>> return(data == val.data);

>>
>> > }

>>
>> The signature is wrong. Use:
>>
>> bool operator==(const testFile &val) const {
>>
>>> However, when I do the following

>>
>>> class testFile2 {
>>> private:

>>
>>> std::vector< testFile<{some-data-type}> > testThis;

>>
>>> public:

>>
>>> bool operator==(testFile2 &this_test) {
>>> return(testThis == this_test.testThis);
>>> }

>>
>> Same here, but vector depend on correct signature.
>>
>> Marcel

>
> Won't using a const lead to a "discards qualifier" error when trying
> to compile?


(a) Why don't you try it and see?
(b) Why would it "discard" qualifier? Are you using "T" with the
operator== that requires it to be non-const?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15614 seconds with 14 queries