Template operators

Discussion in 'C++' started by D-Dog, Apr 7, 2008.

  1. D-Dog

    D-Dog Guest

    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<testFile<float>,
    std::allocator<testFile<float> > >]() == (&__first2)-
    >__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
    D-Dog, Apr 7, 2008
    #1
    1. Advertising

  2. 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
    Marcel Müller, Apr 7, 2008
    #2
    1. Advertising

  3. D-Dog

    Rolf Magnus Guest

    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>::eek:perator* [with _Iterator = const testFile<float>*,
    > _Container = std::vector<testFile<float>,
    > std::allocator<testFile<float> > >]() == (&__first2)-
    >>__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.
    Rolf Magnus, Apr 7, 2008
    #3
  4. D-Dog

    D-Dog Guest

    On Apr 7, 11:23 am, Marcel Müller <>
    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?
    D-Dog, Apr 7, 2008
    #4
  5. D-Dog

    D-Dog Guest

    On Apr 7, 11:34 am, Rolf Magnus <> 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>::eek:perator* [with _Iterator = const testFile<float>*,
    > > _Container = std::vector<testFile<float>,
    > > std::allocator<testFile<float> > >]() == (&__first2)-
    > >>__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.
    D-Dog, Apr 7, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Tibbles

    Template operators

    Alex Tibbles, Jan 23, 2004, in forum: C++
    Replies:
    2
    Views:
    366
    tom_usenet
    Jan 23, 2004
  2. Theodore V. Tolstoy

    Template class operators overloading difficulty

    Theodore V. Tolstoy, Oct 4, 2004, in forum: C++
    Replies:
    2
    Views:
    392
    Rob Williscroft
    Oct 4, 2004
  3. Asfand Yar Qazi
    Replies:
    2
    Views:
    335
    Asfand Yar Qazi
    Nov 27, 2004
  4. Ruben Campos
    Replies:
    5
    Views:
    552
    Ruben Campos
    Nov 30, 2004
  5. Replies:
    3
    Views:
    4,339
Loading...

Share This Page