Declare sortByIndex
static
, because you cannot pass non-static member function to std::sort
.
c++ How to sort an array of structs
Question
I have an array of structs as you can see below.
struct valueCache
{
bool valid;
int startPc;
int index;
f32bit input[12];
f32bit result[12];
};
static const int vcSize = 32;
static valueCache * vc = new valueCache[vcSize];
I want to sort this array in descending order by the index. So I tried this:
myClass.cpp
bool myClass::sortByIndex(const valueCache &lhs, const valueCache &rhs) { return lhs.index > rhs.index; }
bool myClass::sortArray(
{
std::sort(vc, vc + vcSize, sortByIndex);
}
myClass.h
class myClass
{
public:
bool sortByIndex(const valueCache &lhs, const valueCache &rhs);
}
and i get this error
error: argument of type
‘bool (gpu3d::myClass::)(const gpu3d::valueCache&, const gpu3d::valueCache&)’ does not match
‘bool (gpu3d::myClass::*)(const gpu3d::valueCache&, const gpu3d::valueCache&)’
Any ideas how I can fix this?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow