473,499 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
}
May 7 '06 #1
2 19077
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.
May 7 '06 #2
"John" <cy***@purecode.ch> wrote in message
news:11************@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
May 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3970
by: DanielESFA | last post by:
Hey guys :) This is a bit of a funny one... We're four guys working on the same project, everybody using KDevelop and g++ on Linux. Three of us are using Mandrake, with g++ 3.4.3 and 3.4.1....
6
95794
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
12
4862
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
2
2943
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
4
4836
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
17
12737
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
2
2233
by: Thelma Lubkin | last post by:
I use my own matrix and vector classes. I wrote them before such things were generally available and I've stuck with them ever since. I've just added an Octonion class derived from the vectors...
4
8889
by: Andre | last post by:
Hi All, When I compile the following piece of code with gcc, I get 3 "warning: initialization discards qualifiers from pointer target type" messages which refer to the 3 lines marked in the...
8
4407
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.