Create instance of class?
-
I'm trying to update a program that was written in Qt a long time ago (~2001) and update it to Qt6, both as a learning exercise and as a program that would be useful to me. It is written in a collection of modules that handle chunks of the functionality.
There is a serial port component that was written to poll the serial port - I'm trying to change it to be asynchronous. That part seems to be straight forward. The next module 'up the chain' handles data coming from and going to the port. I'm trying to update some connect statements and am having trouble.
Issue is commented in code below:
'pcrio.h'
class PcrIO : public QObject { Q_OBJECT public: explicit PcrIO(); // <--tried making constructor generic //explicit PcrIO( QSerialPort *serialPort, QObject *parent = nullptr );
'pcrproxy.h'
class PcrProxy : public QObject { Q_OBJECT public: PcrProxy( QObject *parent=0, const char *name=0 ); ~PcrProxy(); private: // need to create instance of class //PcrIO( *pcrIO ); PcrIO *pcrIO = new PcrIO();
problem is here, in pcrproxy.cpp:
PcrProxy::PcrProxy(QObject *parent, const char *name) : QObject( ) { // connect to PcrIO to send/receive messages // trying to update the connect statements to 'new' style // error on next line is 'pcrIO is not a class, namespace or enumeration' connect( this, &PcrProxy::sendMessage, &pcrIO, &pcrIO::write()); //connect( this, SIGNAL( sendMessage(const char *, int)), &pcrIO, SLOT( write(const char *, int))); connect( pcrIO, SIGNAL( radioMessage(const char *, int)), this, SLOT( radioMessageSlot(const char *, int)) ); connect( pcrIO, SIGNAL( disconnected() ), this, SLOT( ResetSerialPort() ) );
What am I doing wrong?
BTW - entire project is on github. -
Hi @MScottM,
error on next line is 'pcrIO is not a class, namespace or enumeration'
Your
PcrIO
class has a capitalP
. ie the firstconnect
should be something like:connect( this, &PcrProxy::sendMessage, pcrIO, &PcrIO::write);
Where I've dropped the
&
from the third argument (pcrIO
is already a pointer), replacedpcrIO
withPcrIO
in the fourth argument (to use a class name, not an instance variable), and dropped the()
a the end (its a function pointer, not an invocation).Cheers.
-
Thank you for the answer! I spent way too much time trying to figure it out...