468,746 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,746 developers. It's quick & easy.

Problem with STL sort.

Hi all,
I am using the STL sort to a vector whose every element is an instance
type "struct data" as defined below:

However, when I am trying to compile the code, I am getting the
following error:

/usr/include/c++/3.4.3/bits/stl_algo.h:90: error: passing 'const
SerialNumbers::data' as 'this' argument of 'bool
SerialNumbers::data::operator<(const SerialNumbers::data&)' discards
qualifiers
Can anybody please explain to me what that error means?

I am using the g++ to compile my code.
The code follows:

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <sstream>

using namespace std;

class Test {
public:

struct data
{
string str;

bool operator < (const data& d2)
{

return false;

}

};

vector <stringsortSerials(vector <stringinp)
{
vector<datatable;
for(int i=0; i < inp.size(); ++i)
{
data d;
d.str = inp.at(i);
table.push_back(d);
}

sort(table.begin(),table.end());

vector<stringret;
for(int i=0; i < table.size(); ++i)
{
ret.push_back(table.at(i).str);
}

return ret;

} // the method ends here.
};

Oct 20 '07 #1
2 1641
On 2007-10-20 15:17:12 -0400, alice <al***********@yahoo.comsaid:
>
/usr/include/c++/3.4.3/bits/stl_algo.h:90: error: passing 'const
SerialNumbers::data' as 'this' argument of 'bool
SerialNumbers::data::operator<(const SerialNumbers::data&)' discards
qualifiers
Can anybody please explain to me what that error means?
It means that operator< takes one of its arguments as a non-const
reference. Since the named argument is const&, it must be the other one.
>
bool operator < (const data& d2)
bool operator <(const data& d2) const;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 20 '07 #2
alice wrote:
discards qualifiers
You have to learn what g++ means with its error messages.

"Discards qualifiers" means in at least 90% of cases that a non-const
(usually a function) is being used where a const is expected. In other
words, the function must be const for that specific situation.
Oct 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by jwedel_stolo | last post: by
2 posts views Thread by Joel | last post: by
4 posts views Thread by Johan | last post: by
20 posts views Thread by Xah Lee | last post: by
3 posts views Thread by ritchie | last post: by
20 posts views Thread by Development - multi.art.studio | last post: by
1 post views Thread by thebison | last post: by
8 posts views Thread by SimeonArgus | last post: by
pradeepjain
4 posts views Thread by pradeepjain | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.