Hey all,

Slight continuation on this thread.. Not essential though. Hence the new thread.

The problem is that when I declare a static attribute of a class, and later try using it in a static member function , I get a compile time error.

The problem: (with code)
OscServer.hpp

class OscServer
{
	public:
		OscServer();
		~OscServer();
		
		static sigc::signal<void,std::string> signal_printToConsole;
};

OscServer.cpp

OscServer::OscServer()
{
	// server is declared protected in the header
	server = lo_server_thread_new("5055", error);
	
	// server "default" method 
	lo_server_thread_add_method(server, "/print", "s", printToTextView, NULL);

	lo_server_thread_start(server);
	
}

int OscServer::printToTextView(const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
{
	signal_printToConsole.emit("Called print on SendSignal");
	std::cout << "Emitting signal!" << std::endl;
	return 1;
}

main.cpp

void printFunc (std::string stringToPrint)
{
	std::cout << "printFunc(): " << stringToPrint << std::endl;
}
int main(int argc,char *argv[])
{
	OscServer server;
	server.signal_printToConsole.connect ( sigc::ptr_fun (&printFunc) );
	return 0;
}

The above code gives me the following compile error

In function `OscServer::printToTextView(char const*, char const*, lo_arg**, int, void*, void*)':
oscserver.cpp:(.text+0xeb): undefined reference to `OscServer::signal_printToConsole'

I think this has to do with static / non-static variables, as when I dont declare the sigc::signal<void,std::string> signal_printToConsole; as static, I get invalid use of member ‘OscServer::signal_printToConsole’ in static member function Hope somebody could shed some light on what I've got confused here,
Thanks for reading, -Harry

PS: The sigc::signal<void,std::string> signal_printToConsole comes from the SigC++ signal library

Recommended Answers

All 2 Replies

Static data members aren't defined within the class definition, they're only declared. You need to provide a definition as well:

class foo {
  static int x; // Declaration
};

int foo::x; // Definition

The definition should be in your implementation file rather than your header file if you've done such a split with your class.

Hey Narue,

Cheers, that worked like a charm!
And I assumed that would be illegal to do.. :-)

Thanks, -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.