N
Nelis Franken
Good day.
I'm having trouble telling STL to use a member function to sort items
within the relevant class. The isLess() function needs to have access
to the private members of the Foo class to determine if the one item is
less than the other (it uses two vectors, one containing the actual
data, and one that stores IDs that index into the data vector). The
code below is pretty self-explanatory. I've looked into mem_fun, but
I'm stuck. The same general error seems to occur if I use other
<algorithm> algorithms ("find_if" etc), so it's not something specific
to STL's "sort", but rather on how to let it use the member function
correctly.
I'm using g++ (GCC) 3.4.4 (cygming special), and the error (if you were
to compile the code below) is:
In member function `void Foo::mySort()':
error: no matching function for call to
`sort(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, <unknown type>)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:2576:
note: candidates are: void std::sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator =
If I rewrite:
sort(idList.begin(), idList.end(), isLess)
to read:
sort(idList.begin(), idList.end(), &Foo::isLess)
a long list of STL files (only stl_algo.h reproduced here) report the
following error:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:124:
error: must use .* or ->* to call pointer-to-member function in `__comp
(...)'
Any help will be greatly appreciated.
Thanks!
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class Foo {
public:
Foo() {
for (int i=0; i < 5; i++) {
myList.push_back(i*10);
idList.push_back(i);
}
random_shuffle(myList.begin(), myList.end());
}
bool isLess(int leftID, int rightID) {
return (myList[leftID] < myList[rightID]);
}
void mySort() {
sort(idList.begin(), idList.end(), isLess);
}
private:
vector<int> myList;
vector<int> idList;
};
int main(int argc, char** argv) {
Foo myObj;
myObj.mySort();
return 0;
}
I'm having trouble telling STL to use a member function to sort items
within the relevant class. The isLess() function needs to have access
to the private members of the Foo class to determine if the one item is
less than the other (it uses two vectors, one containing the actual
data, and one that stores IDs that index into the data vector). The
code below is pretty self-explanatory. I've looked into mem_fun, but
I'm stuck. The same general error seems to occur if I use other
<algorithm> algorithms ("find_if" etc), so it's not something specific
to STL's "sort", but rather on how to let it use the member function
correctly.
I'm using g++ (GCC) 3.4.4 (cygming special), and the error (if you were
to compile the code below) is:
In member function `void Foo::mySort()':
error: no matching function for call to
`sort(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, <unknown type>)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:2576:
note: candidates are: void std::sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator said:, _Compare = bool (Foo::*)(int, int)]
If I rewrite:
sort(idList.begin(), idList.end(), isLess)
to read:
sort(idList.begin(), idList.end(), &Foo::isLess)
a long list of STL files (only stl_algo.h reproduced here) report the
following error:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:124:
error: must use .* or ->* to call pointer-to-member function in `__comp
(...)'
Any help will be greatly appreciated.
Thanks!
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class Foo {
public:
Foo() {
for (int i=0; i < 5; i++) {
myList.push_back(i*10);
idList.push_back(i);
}
random_shuffle(myList.begin(), myList.end());
}
bool isLess(int leftID, int rightID) {
return (myList[leftID] < myList[rightID]);
}
void mySort() {
sort(idList.begin(), idList.end(), isLess);
}
private:
vector<int> myList;
vector<int> idList;
};
int main(int argc, char** argv) {
Foo myObj;
myObj.mySort();
return 0;
}