Asmozan Asmozan - 1 month ago 11
C++ Question

C++ Vector sort odd and even stay

whats wrong about this code? I want to sort odd numbers but even stay int their places, made function for this but compilator wont pass this

There is error:


error: reference to non-static member function must be called


and there is a code:

public:
std::vector<int> sortArray(std::vector<int> array)
{
std::vector<int> sortedArray(array);
std::sort ( sortedArray.begin() , sortedArray.end() , oddSort );
}

bool oddSort ( const int& left , const int& right ){
if ( left % 2 && right % 2 )
return left < right;
else if ( left % 2 )
return false;
else if ( right % 2 )
return true;
return left < right;
}

Answer

Danh's comment is right, but this is actually a duplicate question. However, there are no duplicates with accepted answers yet.

So: the problem is that member functions need a this pointer, unless they're static. And std::sort won't give you a this pointer, nor do you need one. So making it static is the straightforward solution.