C++ STL vector sorting objects in GCC

Started by
3 comments, last by Zahlman 17 years, 6 months ago
Hi I'm trying to port code over from visual C++ to GCC, but I find that I'm having trouble sorting objects in a vector. I have trouble compiling this code with GCC, but it works fine for me in VC++.

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

using namespace std;

class cHash{
          int key;
            double value;
        public:

            bool operator<(const cHash &a){
                double v;
                cHash a1=a;
                v=a1.getValue();
                return value<v;

            }

            double getValue();
            int getKey();

            void setKey(int k);
            void setValue(double v);
};



int main(void){
        vector<cHash> v1;

        cHash c;
        c.setKey(0);
        c.setValue(1);

        v1.push_back(c);

        c.setKey(1);
        c.setValue(2);
        v1.push_back(c);

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

        return 0;
}
I get this error:

/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                 In function "const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with                 _Tp = cHash]":
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                2498:   instantiated from "void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<cHash*, std::vector<cHash, std::allocator<cHash> > >, _Size = int]"
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                2569:   instantiated from "void std::sort(_RandomAccessIterator, _RandomAccessIt                erator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<cHash*, std::vector<cHash, std::allocator<cHash> > >]"
helloworld.cpp:44:   instantiated from here
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                90: error: passing "const cHash" as "this" argument of "bool cHash::operator<(const cHash&)" discards qualifiers
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                91: error: passing "const cHash" as "this" argument of "bool cHash::operator<(const cHash&)" discards qualifiers
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                93: error: passing "const cHash" as "this" argument of "bool cHash::operator<(const cHash&)" discards qualifiers
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                97: error: passing "const cHash" as "this" argument of "bool cHash::operator<(const cHash&)" discards qualifiers
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_algo.h:                99: error: passing "const cHash" as "this" argument of "bool cHash::operator<(const cHash&)" discards qualifiers
Advertisement
You can only call const member functions on const objects. See here for more detailed information.

As an aside, your < is pretty awkward. All you really need is return value < a.value.

*edit: PS, the reason this works on VC++ isn't some failure on MSs part. Their implementation doesn't create any const references, so a mutating operator< would be valid. gcc apparently makes use of some function called median, which accepts a const reference, so a mutating operator< would be bad.

CM
bool operator<(const cHash &a) const{	double v;	cHash a1=a;	v=a1.getValue();	return value < v;}

Which could just be implemented as:
bool operator<(const cHash &a) const {	return value < a.value;}

Σnigma
That worked....

I could have sworn I did just this... but I guess I need to get my eyes checked.
The fact that you're making a vector of structures that are "key" and "value" pairs, and sorting the vector, makes me *very* suspicious. You *are* aware of std::map, yes?

This topic is closed to new replies.

Advertisement