Jump to content
  • Advertisement
Sign in to follow this  
khzilla

C++ STL vector sorting objects in GCC

This topic is 4453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!