473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1831
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
2
by: Joel | last post by:
I am having some problems compiling my code on Mandrake 10 with g++ (GCC 3.3.2). The problem seems to be in that I try to define a functor that compares two pointer objects, and use that functor to...
4
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
1
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
8
by: SimeonArgus | last post by:
I need to sort a list of points, so I've considered using an IComparable implementation. Should be easy, right? But I need to know two things in the CompareTo function, not one. Test1: I need to...
10
by: shubha.sunkada | last post by:
Hi, I have a recordset connection in asp that I am using to search records.If I use the client side cursorlocation (rs.cursorlocation=3) then it takes really long to return back the records due...
4
pradeepjain
by: pradeepjain | last post by:
i had posted the same code in javascript area !! this i am posting bcos of different problem. as you can see in ma code there is a dropdown called sort ...its sorts the result by the value he...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.