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