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
EG:
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

post code of the class and the method that is causing the problem.

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.

main.cpp

#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
{
	public:
		OscServer()
		{
			server = lo_server_thread_new("8888", error);
		}
		
		~OscServer()
		{
			std::cout << "~OscServer" <<  std::endl;
		}
		
		bool start()
		{
			lo_server_thread_start(server);
			return true;
		}
	
	private:
		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;
	server.start();
	
	std::cout << "Ready!" << std::endl;
	
	while (!done) 
	{
		usleep(1000);
	}
	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*)’

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.

Great to understand & learnt that.. :-) Thank you, -Harry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.