discards qualifiers



 DEVELOP > c-Plus-Plus > discards qualifiers

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "John"
Date: 06 May 2006 09:31:48 PM
Object: discards qualifiers
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>::operator == (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>::operator < (Pair<T1, T2> p)
{
return first < p.first || (!(p.first < first) && second < p.second);
}
.

User: "John Carson"

Title: Re: discards qualifiers 06 May 2006 10:27:57 PM
"John" <cybix@purecode.ch> wrote in message
news:1146961898_128@sicinfo3.epfl.ch

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>::operator == (Pair<T1, T2> p)

bool Pair<T1, T2>::operator == (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>::operator < (Pair<T1, T2> p)

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

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

--
John Carson
.

User: "Ian Collins"

Title: Re: discards qualifiers 06 May 2006 07:48:03 PM
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>::operator == (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>::operator < (Pair<T1, T2> p)
{
return first < p.first || (!(p.first < first) && second < p.second);
}

--
Ian Collins.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER