Problem with STL sort.

A

alice

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::eek:perator<(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 <string> sortSerials(vector <string> inp)
{
vector<data> table;
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<string> ret;
for(int i=0; i < table.size(); ++i)
{
ret.push_back(table.at(i).str);
}

return ret;

} // the method ends here.
};
 
P

Pete Becker

/usr/include/c++/3.4.3/bits/stl_algo.h:90: error: passing 'const
SerialNumbers::data' as 'this' argument of 'bool
SerialNumbers::data::eek:perator<(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;
 
J

Juha Nieminen

alice said:
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.
 
A

Andre Kostur

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::eek:perator<(const SerialNumbers::data&)' discards
qualifiers

You're discarding a const qualifier. Your operator< isn't a const-method
of the class. As a result, you can't compare const instances (or
references) of your class.

Also, your comparison function doesn't specify a strict weak ordering of
your objects.... (I presume the body should be: "return str < d2.str;")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top