|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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==(conststd::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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 . |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|