Template operators

D

D-Dog

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::eek: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>::eek:perator* [with _Iterator = const testFile<float>*,
_Container = std::vector said:
__gnu_cxx::__normal_iterator<_Iterator, _Container>::eek: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>::eek: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
 
M

Marcel Müller

D-Dog said:
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

Rolf Magnus

D-Dog said:
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>::eek:perator* [with _Iterator = const testFile<float>*,
_Container = std::vector said:
__gnu_cxx::__normal_iterator<_Iterator, _Container>::eek: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>::eek: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.
 
D

D-Dog

The signature is wrong. Use:

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




Same here, but vector depend on correct signature.

Marcel

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

D-Dog

D-Dog said:
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>::eek:perator* [with _Iterator = const testFile<float>*,
_Container = std::vector said:
__gnu_cxx::__normal_iterator<_Iterator, _Container>::eek: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>::eek: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 help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top