discards qualifiers

Discussion in 'C++' started by Ian Collins, May 7, 2006.

  1. Ian Collins

    Ian Collins Guest

    John wrote:
    > Hello!
    >
    > When I compile the following code, I get this error message "error:
    > passing ... discards qualifiers" and I don't understand why.
    >
    > Could anybody help me?
    >

    See comments inline.

    > Thank you!
    >
    > John
    >
    >
    > -----main.cpp------
    > ...
    > map<Pair<string, string>, int> myMap;
    > myMap(Pair<string, string>("bla", "blablabla")) = 2;


    use qualified names, std::map, std::string.
    > ...
    >
    > -----Pair.h-------
    > template <class T1, class T2>
    > class Pair
    > {
    > public:
    > T1 first;
    > T2 second;
    > Pair();
    > Pair(T1 first, T2 second);
    > virtual ~Pair();
    > bool operator == (Pair<T1, T2> p);
    > bool operator < (Pair<T1, T2> p);


    These should both be (const Pair& p) const.

    > };
    >
    > -----Pair.cpp-------
    > #include "Pair.h"
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::pair()
    > {
    > }
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::pair(T1 first, T2 second)
    > {
    > this->first = first;
    > this->second = second;


    Prefer initialiser lists.

    > }
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::~Pair()
    > {
    > }
    >
    > template <class T1, class T2>
    > bool Pair<T1, T2>::eek:perator == (Pair<T1, T2> p)
    > {
    > return (first == p.first && second == p.second) || (first == p.second
    > && second == p.first);
    > }
    >
    > template <class T1, class T2>
    > bool Pair<T1, T2>::eek:perator < (Pair<T1, T2> p)
    > {
    > return first < p.first || (!(p.first < first) && second < p.second);
    > }



    --
    Ian Collins.
     
    Ian Collins, May 7, 2006
    #1
    1. Advertising

  2. Ian Collins

    John Guest

    Hello!

    When I compile the following code, I get this error message "error:
    passing ... discards qualifiers" and I don't understand why.

    Could anybody help me?

    Thank you!

    John


    -----main.cpp------
    ....
    map<Pair<string, string>, int> myMap;
    myMap(Pair<string, string>("bla", "blablabla")) = 2;
    ....

    -----Pair.h-------
    template <class T1, class T2>
    class Pair
    {
    public:
    T1 first;
    T2 second;
    Pair();
    Pair(T1 first, T2 second);
    virtual ~Pair();
    bool operator == (Pair<T1, T2> p);
    bool operator < (Pair<T1, T2> p);
    };

    -----Pair.cpp-------
    #include "Pair.h"

    template <class T1, class T2>
    Pair<T1, T2>::pair()
    {
    }

    template <class T1, class T2>
    Pair<T1, T2>::pair(T1 first, T2 second)
    {
    this->first = first;
    this->second = second;
    }

    template <class T1, class T2>
    Pair<T1, T2>::~Pair()
    {
    }

    template <class T1, class T2>
    bool Pair<T1, T2>::eek:perator == (Pair<T1, T2> p)
    {
    return (first == p.first && second == p.second) || (first == p.second
    && second == p.first);
    }

    template <class T1, class T2>
    bool Pair<T1, T2>::eek:perator < (Pair<T1, T2> p)
    {
    return first < p.first || (!(p.first < first) && second < p.second);
    }
     
    John, May 7, 2006
    #2
    1. Advertising

  3. Ian Collins

    John Carson Guest

    "John" <> wrote in message
    news:
    > Hello!
    >
    > When I compile the following code, I get this error message "error:
    > passing ... discards qualifiers" and I don't understand why.
    >
    > Could anybody help me?
    >
    > Thank you!
    >
    > John
    >
    >
    > -----main.cpp------
    > ...
    > map<Pair<string, string>, int> myMap;
    > myMap(Pair<string, string>("bla", "blablabla")) = 2;


    The standard library includes a templated functional called less. less is a
    struct that has an operator() that takes two arguments and applies < to
    them. The two arguments are declared as const, which means that it is not
    permissible to call any operator on them which is not likewise const.
    Calling a non-const operator "discards qualifiers", i.e., discards const.

    By default, map uses less to compare keys. less is calling your operator<.
    Since your operator< is non-const, you are getting the error message.

    >
    > -----Pair.h-------
    > template <class T1, class T2>
    > class Pair
    > {
    > public:
    > T1 first;
    > T2 second;
    > Pair();
    > Pair(T1 first, T2 second);
    > virtual ~Pair();
    > bool operator == (Pair<T1, T2> p);


    bool operator == (const Pair<T1, T2> p) const;

    (the first const says you aren't going to modify the "other" Pair; the
    second const says the Pair isn't going to modify itself.) You need to make
    the same changes to the implementation --- see below

    > bool operator < (Pair<T1, T2> p);


    bool operator < (const Pair<T1, T2> p) const;

    > };


    Unless your compiler supports export, this separation into .h and .cpp files
    won't work. You need to put the implementation into the .h file when using
    templates (more precisely, the implementation needs to be visible in the
    translation unit that is using it --- the compiler has to be able to see it;
    you can't rely on the linker).

    > -----Pair.cpp-------
    > #include "Pair.h"
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::pair()
    > {
    > }
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::pair(T1 first, T2 second)
    > {
    > this->first = first;
    > this->second = second;
    > }
    >
    > template <class T1, class T2>
    > Pair<T1, T2>::~Pair()
    > {
    > }
    >
    > template <class T1, class T2>
    > bool Pair<T1, T2>::eek:perator == (Pair<T1, T2> p)


    bool Pair<T1, T2>::eek:perator == (const Pair<T1, T2> p) const

    > {
    > return (first == p.first && second == p.second) || (first == p.second
    > && second == p.first);
    > }
    >
    > template <class T1, class T2>
    > bool Pair<T1, T2>::eek:perator < (Pair<T1, T2> p)


    bool Pair<T1, T2>::eek:perator < (const Pair<T1, T2> p) const

    > {
    > return first < p.first || (!(p.first < first) && second < p.second);
    > }



    --
    John Carson
     
    John Carson, May 7, 2006
    #3
    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. danny van elsen
    Replies:
    6
    Views:
    41,251
    Pete Becker
    Apr 24, 2005
  2. Jason
    Replies:
    6
    Views:
    9,073
    Mike Wahler
    Oct 9, 2003
  3. Charlie Zender
    Replies:
    12
    Views:
    1,098
    Peter Pichler
    Jan 3, 2004
  4. Neo
    Replies:
    1
    Views:
    558
    Victor Bazarov
    Mar 28, 2006
  5. Markus Dehmann
    Replies:
    2
    Views:
    586
    Daniel T.
    Aug 22, 2006
Loading...

Share This Page