Hey all,

I've tried many times to write a C++ class around a C library to make a nice
clean interface to that library.

Now I keep on getting the same problem:
Once in the class, I initialize the C library, and then the C library complains that
error converting:
void (*)(int example) to
void (MyClass:: *) (int example)

I've been stuck on this one for a good 2 months now, anybody out there had the same problem? I'd appreciate any suggestions!

Cheers, -Harry

7 Years
Discussion Span
Last Post by harryhaaren

Hey Dragon,

Sorry bout the 50 lines.. but it think this file best explains my problem.
I'm using a library called LibLO, a lightweight OSC library.

As i said before, I'm trying to create a Class for around the C functions,
to have nicer access to each function, and keep the #include <lo/lo.h> out of my global namespace.


#include <iostream>
#include <cstdlib>
#include <lo/lo.h>	// LIBLO, an Open Sound Control library

// Im running linux, so for me the compile command is:
// g++ oscserver.cpp main.cpp `pkg-config liblo --cflags --libs`

class OscServer
			server = lo_server_thread_new("8888", error);
			std::cout << "~OscServer" <<  std::endl;
		bool start()
			return true;
		lo_server server;
		void error(int num, const char *msg, const char *path)
			std::cout << "liblo server error " <<num << " in path "<< path << msg << std::endl;

int main()
	int done = 0;
	OscServer server;
	std::cout << "Ready!" << std::endl;
	while (!done) 
	return 0;

And here's my error:

harry@audevinux:/mnt/sda4/programmin/cpp/OscServer$ g++  main.cpp `pkg-config liblo --cflags --libs`
main.cpp: In constructor ‘OscServer::OscServer()’:
main.cpp:15: error: argument of type ‘void (OscServer::)(int, const char*, const char*)’ does not match 
                                   ‘void (*)(int, const char*, const char*)’

Edited by harryhaaren: Adding the error i get when I compile


Just as I suspected. Only static methods can be passed to that C function. Make the function error static and it will probably compile ok.


Cheers, That compiled no problem.

Just to make sure i leant the right thing, Any function i pass to a C function I call from inside a C++ class must be static?

(Sorry for the confuzing question.. :P)
Thanks again! -Harry


That is correct, because static class functions are just like any other global function except for the class scope identifier.

Edited by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.