473,566 Members | 2,776 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<stri ng, 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 19086
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<stri ng, 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.e pfl.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<stri ng, 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
3979
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. Project compiles nicely on these three puters. On the fourth computer, running SUSE 9.3 using KDevelop also, but g++ 3.3.5, and on the school's Debian...
6
95811
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 pointer target type Does anyone know what this warning means? Why do I get it? The program compiles and appears to work, but I'd like to understand...
12
4867
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 -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
2
2954
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
4848
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 _Predicate::argument_type argument_type; typedef typename _Predicate::return_type return_type; _Predicate pred_;
17
12747
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 target type" when I *access* a member of an argument defined as const? Please see the code below:
2
2243
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 class and I'm trying to do some matrix arithmetic where the elements of the matrices are Octonions. (My Octonions are essentially 8-component vectors...
4
8899
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 code. What's wrong with these lines? I searched the web for it, but didn't find anything useful. Thanks,
8
4412
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 looks like this header file
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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 we have to send another system
0
925
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.