PDA

View Full Version : Connecting to a signal that does not have a pointer to the class



philwinder
15th November 2008, 20:35
Hi again,
I'm having problems connecting to a class that does not have a pointer.
I created an instance of a class and I thought that I didn't need it to be a pointer, so I will just create the object in my header file. But when I attempt to connect a signal from that instance the compiler throws an error:


xBeeComms comms(); // Object
...
connect( &comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) ); // In the constructor

The error I get is:

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&messageGenerator::comms’

So I change the code to:

connect( &messageGenerator::comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) );
And I now get the error:

error: no matching function for call to ‘messageGenerator::connect(xBeeComms (messageGenerator::*)(), const char [30], messageGenerator* const, const char [30])’
Any idea's how to get it working?

weepdoo
15th November 2008, 20:58
The problem with just a simple object (as you do create it) is that it will be destroyed when you will leave the current block (probably your method here).

So, you ask Qt to connect a signal from a object that is going to be destroyed.

What you could write might be something like:


#
xBeeComms *comms = xBeeComms(); // Object
#
...
#
connect( comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) ); // In the constructor

delete comms; // <- really if you need it to be destroyed

philwinder
16th November 2008, 12:23
Sorry, its declared in the header file, meaning that it should be available for the entire life of the class. And since thats the only amount of time I require it for, thats ok.

I know I could just create a pointer to that class and create a new object on the heap with new, but I dont think we should have to?

Phil

aamer4yu
16th November 2008, 12:41
The problem is with the declaration.

xBeeComms comms(); // Object

Its NOT an object.... It is a function prototype... telling compiler that comms is a function of return type xBeeComms.

Just write it simply as -
xBeemComms comms;

:)

philwinder
16th November 2008, 13:53
Huh, silly me! I got an error because I didnt provide a default parameter so I automatically put the brackets in for some reason. Sorry!

Thanks!