Jose Carlos Garcia Sogo: GCompareFunc...
or why C++ sucks.
I am wondering if someone could be able to explain me why this
piece of code fails when I try to compile it:
- GMJabberClient *GMJabberManager::GetClient (GmAccount *ac)
- {
- GMJabberClient *client = NULL;
-
- client = g_slist_find_custom (jabber_client_list, ac,
- (GCompareFunc) compare_clients_by_account);
- return client;
-
- }
-
- gint GMJabberManager::compare_clients_by_account (gconstpointer a,
- gconstpointer b)
- {
- GmAccount *client_ac;
-
- GMJabberClient *client = (GMJabberClient *) a;
- GmAccount *ac = (GmAccount *) b;
-
- client_ac = client->GetGmAccount();
- if ( strcmp(client_ac->aid, ac->aid) )
- return 0;
-
- return 1;
- }
It fails at g_slist_find_custom call, why GCC saying:
endpoints/jabber.cpp: In member function GMJabberClient* GMJabberManager::GetClient(GmAccount*): endpoints/jabber.cpp: 6 error: invalid use of member (did you forget the & ?) make[1]: *** [jabber.o] Error 1I have been only able to fix this by declaring compare_clients_by_account () function a static function, while it actually should be private method of GMJabberManager client. Ideas?
Update 04-21-2006 11:31:34.- First of all, thanks to everybody who have
replied to me by mail. Fot those who wonder why I don't have comments enabled,
the answer is easy... it is another place to look at, to keep clean of spam,
and to care.
Those who suggested using constructions like
this->compare_clients_by_account
or &compare;_clients_by_account that won't work, as other people
explained to me (and GCC says a bit more clearly if you try to compile that).
As this is C++, when using such constructions you get a member function
which gets passed a pointer to the class instance. This way, the prototype
for what g_slist_find_custom expects and what it is
actually being passed is not the same.
This is where
static member functions enter in action. Those are functions belonging to
a class that not get passed the this pointer, avoiding the
problem.
Please, follow next week our "C++ sucks / what the hell" show!