From: Gad Zooks on

Hi,

Can anybody please help me with the correct syntax for using Sort()
with an Object Array, I am getting myself in a complete muddle with
the whole CMPFUNC thing.

The manual says:

wxArray::Sort
void Sort(CMPFUNC<T> compareFunction)

The notation CMPFUNC<T> should be read as if we had the following
declaration:


template int CMPFUNC(T *first, T *second);

where T is the type of the array elements. I.e. it is a function
returning int which is passed two arguments of type T *.

But I guess I'm not understanding something; as I said, it's not idiot
proof!

I'd be very grateful if anybody has a working snippet of code, I have
searched the sample code tree without any success.

Many Thanks,

Gad

From: Dave Symonds on
On 6/22/05, Gad Zooks <gad_zooks(a)hotmail.co.uk> wrote:
>
> template int CMPFUNC(T *first, T *second);
>
> where T is the type of the array elements. I.e. it is a function
> returning int which is passed two arguments of type T *.

CMPFUNC is a function that takes two elements of the array, and
determines which order they should go in. If you were sorting strings
alphabetically it should be coded so that:

CMPFUNC("Apple", "Bear") => -1
CMPFUNC("Cake", "Cake") => 0
CMPFUNC("Rock", "Gem") => 1

That help?


Dave.

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: Gad Zooks on
On Wed, 22 Jun 2005 00:17:28 +0000 (UTC), dsymonds(a)gmail.com (Dave
Symonds) wrote:
>
>CMPFUNC is a function that takes two elements of the array, and
>determines which order they should go in. If you were sorting strings
>alphabetically it should be coded so that:
>
>CMPFUNC("Apple", "Bear") => -1
>CMPFUNC("Cake", "Cake") => 0
>CMPFUNC("Rock", "Gem") => 1
>
>That help?
>

Ummmm ... no!

I'm sorry if I am being a bit dim or even a lot dim, but please bear
with me as I am desperate to fix this problem.

I have a class Dictionary which contains two wxChar fields and I have
made an object array DictionaryArray from the class.

I originally wrote the following comparison function, expecting
similar behaviour to qsort() :

int compare_func( Dictionary *A, Dictionary *B )
{
int res;

if((res = strcmp( A->field1, B->field1 )))
return(res);

return( strcmp( A->field2, B->field2));
}

Having successully populated DictionaryArray I then wanted to issue
the following call:

DictionaryArray.Sort( compare_func );

much in the same manner as qsort() but this does not compile.

Is the compare_func a method of the Dictionary class ?

I have tried changing the declaration to:

CMPFUNCDictionary( Dictionary *A, Dictionary *B )
{
....
}

which seems to compile but then when I try to call sort thus:

DictionaryArray.Sort( CMPFUNCDictionary );

Again I get compilation errors. I have tried every combination I can
think of and am no nearer getting the code to compile.

What am I doing wrong ?

If you or anybody else have a code fragment showing how to make the
CMPFUNC declaration and calls to the Sort() function that I could look
at I would be very grateful.

Thanks for your help,

Gad
From: Dave Symonds on
On 6/22/05, Gad Zooks <gad_zooks(a)hotmail.co.uk> wrote:
> Again I get compilation errors. I have tried every combination I can
> think of and am no nearer getting the code to compile.

What compile errors? Post them!


Dave.

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: Gad Zooks on
On Wed, 22 Jun 2005 11:40:57 +0000 (UTC), dsymonds(a)gmail.com (Dave
Symonds) wrote:

>
>What compile errors? Post them!
>


In my previous examples I had tried to avoid giving any real detail of
my code as I was certain that it was my understanding that was at
fault and I didn't want to complicate things by inclding my rather
messy and complicated classes, so I have bashed up a very quick sample
to generate the compile errors I am getting without going into the
detail of my code.

Thus, In example.h:

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif //WX_PRECOMP

/* ---------------------------------------------------------
** Example
*/

class Example : wxObject
{
public:
Example();

wxChar field1[ 10 ];
wxChar field2[ 10 ];
} ;

in example.cpp:


#include "example.h"

#include <wx/dynarray.h>

WX_DECLARE_OBJARRAY( Example, wxArrayOfExamples );

#include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY( wxArrayOfExamples );


// Constructor
Example::Example()
{
field1[0] = NULL;
field2[0] = NULL;

return;
}

/*!
* Utility function to sort rows by their values
*/

int CMPFUNCExample( const Example *A, const Example *B )
{
int res;

if( res = strcmp( A->field1, B->field1 ))
return( res );

return( strcmp( A->field2, B->field2 ) );
}

void CallSort()
{
Example *Ex;
wxArrayOfExamples *ExArray = new wxArrayOfExamples;

Ex = new Example;
strcpy( Ex->field1, "A" );
ExArray->Add( Ex );

Ex = new Example;
strcpy( Ex->field1, "B" );
ExArray->Add( Ex );

ExArray->Sort( CMPFUNCExample );
}



Which generates the following error:

C:\DataLynx\Development\DataLynx_GUI\DataLynxETL_v2\example.cpp(57) :
error C2664: 'Sort' : cannot convert parameter 1 from
'int (const class Example *,const class Example *)'
to
'int (__cdecl *)(class Example ** ,class Example ** )'
None of the functions with this name in scope match the target type


I changed the declaration on CMPFUNC thus to use pointers to pointers:

int CMPFUNCExample( Example **A, Example **B )
{
int res;

if( res = strcmp( (*A)->field1,(*B)->field1 ))
return( res );

return( strcmp( (*A)->field2, (*B)->field2 ) );
}

and the compile error becomes:

C:\DataLynx\Development\DataLynx_GUI\DataLynxETL_v2\example.cpp(51) :
error C2664: 'Sort' : cannot convert parameter 1 from
'int (const class Example ** ,const class Example ** )'
to
'int (__cdecl *)(class Example ** ,class Example ** )'
None of the functions with this name in scope match the target type


So... changed the declaration on CMPFUNC thus (losing the const's):

int CMPFUNCExample( Example **A, Example **B )
{
int res;

if( res = strcmp( (*A)->field1,(*B)->field1 ))
return( res );

return( strcmp( (*A)->field2, (*B)->field2 ) );
}

and .... it compiles ..... <furtively tests that it is working > ....
um .... and works ....

I can't believe it - I was at this til 1am last night!

Well, boy, do I feel sheepish now! I did say it wasn't idiot proof !!!

Actually I do think that I am still not using this as it was intended;
the manual says :

wxArray::Sort
void Sort(CMPFUNC<T> compareFunction)

The notation CMPFUNC<T> should be read as if we had
the following declaration:

template int CMPFUNC(T *first, T *second);

where T is the type of the array elements. I.e. it is a
function returning int which is passed two arguments
of type T *.

and I am somehow having to use pointers to pointers, is there still
something I am doing wrong ?

Dave, I am really grateful for your time, if it hadn't been for you I
would never have stumbled accross the solution.

I'd like to be able to say that if ever I can help you with a problem
you are having .... but on this basis it's not likely is it ?

Many Many Thanks,

gad
<small g to indicate acute embarassment>

 |  Next  |  Last
Pages: 1 2 3
Next: determining image resolution (dpi)