Connecting to a signal that does not have a pointer to the class
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:
Code:
xBeeComms comms(); // Object
...
connect( &comms,
SIGNAL( dataReceived
( QbyteArray
* ) ),
this,
SLOT( receivedData
( QByteArray * ) ) );
// In the constructor
The error I get is:
Code:
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:
Code:
connect( &messageGenerator
::comms,
SIGNAL( dataReceived
( QbyteArray
* ) ),
this,
SLOT( receivedData
( QByteArray * ) ) );
And I now get the error:
Code:
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?
Re: Connecting to a signal that does not have a pointer to the class
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:
Code:
#
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
Re: Connecting to a signal that does not have a pointer to the class
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
Re: Connecting to a signal that does not have a pointer to the class
The problem is with the declaration.
Quote:
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;
:)
Re: Connecting to a signal that does not have a pointer to the class
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!