Strange vector sort (with compage function) errors

Hi guys,

I’m trying to sort blobs on there x position.
But I can’t get the sort function to work. I have tried all the commented kind of calls.

  
void sortBlobs(vector<ofxCvBlob*> * blobs)  
{  
	//std::sort (blobs->begin(), blobs->end(), blobsXSort);  
	//std::sort (blobs->begin(), blobs->end(), &blobsXSort);  
	std::sort (blobs->begin(), blobs->end(), &VideoInputController::blobsXSort);  
	//std::sort (blobs->begin(), blobs->end(), VideoInputController::blobsXSort);  
	//std::sort (blobs->begin(), blobs->end(), *VideoInputController::blobsXSort);  
	//qsort (blobs,blobs.size(), &VideoInputController::blobsXSort);  
}  
bool blobsXSort(ofxCvBlob * blob1, ofxCvBlob * blob2)  
{   
	return (blob1->boundingRect.x < blob2->boundingRect.x);   
}  

The not commented one gives me the most hope (that’s also the syntax we use for event handlers). But it gives me the following error in the stl_algo.h class:

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_algo.h:125:0 /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_algo.h:125: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘__comp (…)’

I don’t have a clue what to do anymore.
Do you guys have any tips?

Thnx!

Peter Uithoven

the std sorting functions expect a function non a member method, so you cannot use:

&VideoInputController::blobsXSort

but blobXSort, that should be a function outside any class.

Then i get the not declared in this scope error.
I have out the sort function outside the class (but in the same file) and I refer to the function like this.

  
blobsXSort  

What could I be doing wrong?

do you have the function declaration before the call?

Hi Arturo,
Apologies for my (example-less) explanation, I was on my way home at that time.

I eventually did, what always helps me, create a very simple example to test in.

I basically got it working in 10min by placing the custom sort function above the function that called std:sort in the .cpp file.

To help others I will now list the error codes for people with the same kind of issue.

Custom sort function after the function that calles std:sort:

error: ‘sortOnNumPages’ was not declared in this scope

Custom sort function in the .h file, outside the class:

Command /Developer/usr/bin/g+±4.2 failed with exit code 1

Custom sort function in the .h file, inside the class:

error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘__comp (…)’

Working example:
testApp.h

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "Book.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		vector<Book*> * library;  
		void setup();  
};  
  
#endif  
  

testApp.cpp

  
#include "testApp.h"  
  
bool sortOnNumPages(Book * book1, Book * book2)  
{  
	return (book1->numPages < book2->numPages);  
}  
void testApp::setup()  
{  
	cout << "testApp::setup\n";  
	cout << "  create \n";  
	library = new vector<Book*>;  
	for (int i=0; i<5; i++)   
	{  
		Book * book = new Book();  
		cout << "    book->numPages: " << book->numPages << "\n";  
		library->push_back(book);  
	}  
	  
	cout << "  sort\n";  
	//sort  
	//std:sort(library->begin(),library->end());  
	std:sort(library->begin(),library->end(),&sortOnNumPages);  
	for (int i=0; i<5; i++)   
	{  
		Book * book = library->at(i);  
		cout << "    book->numPages: " << book->numPages << "\n";  
	}  
}  

Book.h

  
#ifndef _Book  
#define _Book  
  
#include "ofMain.h"  
  
class Book  
{  
	public:  
		Book();  
		int numPages;  
};  
  
#endif  
  

Book.cpp

  
#import "Book.h"  
  
  
Book::Book()  
{  
	numPages = ofRandom(0, 10);  
}  

Thanx for your time Arturo.