0

I cam from programming in Java and I thought doing as here [my code] could work the definition of temporal constructors, but I am having problems the proper way of writing the code to just work.

PROBLEM: I want to add a Constructor of the class Coomunication for every port I find in a machine and save it into a vector:

ControlCommunication.cpp:

...
QVector<Comunication *> ports;
...
void ControlCommunication::checkPorts(){
  qint16  vendorid = cuquito.getVendorID();
  qint16 productid = cuquito.getProductID();
  const QString blankString = "N/A";

  for (const QSerialPortInfo &serialPortInfo : QSerialPortInfo::availablePorts()) {
    Comunication com(serialPortInfo.portName());
    addPort(com); // <- I do not know how to get this working well

  }
}

ControlCommunication.h:

class ControlCommunication : public QObject
{
  Q_OBJECT
public:
  explicit ControlCommunication(QObject *parent = nullptr);
  QVector<Comunication*> getComunicationPorts(){return comunicationports;}
  void addPort(Comunication com);
...

Comunication.h:

#ifndef COMUNICATION_H
#define COMUNICATION_H

#include <QObject>
#include <QSerialPort>
#include <QMainWindow>

class Comunication:public QObject{
  Q_OBJECT

public:
  Comunication( QString serialPortName);
  Comunication();

public:

public slots:
  void openSerialPort();
  void closeSerialPort();
  void writeData(const QByteArray &data);
  void readData(QByteArray &data);
  void handleError(QSerialPort::SerialPortError error);
  QString getPortName(){return portname;};
signals:

private:
  QSerialPort *m_serial = nullptr;
  QString portname;
};

#endif // COMUNICATION_H

The error I get is: call to implicitly-deleted copy constructor of 'Comunication' comunication.h:8:20: note: copy constructor of 'Comunication' is implicitly deleted because base class 'QObject' has a deleted copy constructor qobject.h:449:20: note: 'QObject' has been explicitly marked deleted here controlcommunication.cpp:27:49: note: passing argument to parameter 'com' here

As I understand from the error message, I cannot call a constructor which is gonna be deleted in that function from other statements. How can i get this working?

luarpy
  • 1
  • 2

2 Answers2

0

The error you're getting mean you can't copy Communication. When you're passing com to addPort, you're making a copy of the object which will be passed to the function. But because you're inheriting a QObject you can't make copies of your objects. Which means you can't those objects by value. You can add pointers or (ideally) a reference to use objects.

But in your case, I don't think that's going to work easily, you seem to want a QVector<Communication*> which I'm assuming will be filled by addPort. But you're creating Communication in your loop which will be delted when it exists the loop, and so pointers to it won't work.

I don't have enough information, and I'm not versed with Qt, so please someone correct if I'm wrong, but I would suggest that you build Communication inside addPort and change QVector<Communication*> to QVector<Communication>.

My intuition would be that Communication doesn't need to inherit from QObject, but I could be wrong. But the data from your program should probably be separated from the Qt stuff, to make things easier to manage.

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24
0

You are passing the Comunication object by value into the addPort function. This will try to create a copy of the object. But QObjects (and anything derived from QObject) are non-copyable. So you should change your function to take a pointer or reference instead.

Also, you probably need to allocate your objects on the heap because they will go out of scope, freeing any stack memory.

void addPort(Communication *com);

...

  for (const QSerialPortInfo &serialPortInfo : QSerialPortInfo::availablePorts()) {

    // Allocate on the heap
    Comunication *com = new Comunication(serialPortInfo.portName());

    // Pass pointer to addPort
    addPort(com);

  }
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • Sure. See [QSharedPointer](https://doc.qt.io/qt-5/qsharedpointer.html), etc. – JarMan Jul 07 '21 at 15:34
  • @MooingDuck: Yes it support it, but it has its own ownership policy: to most classes, you might pass `parent` which will be the owner of the created instance. – Jarod42 Jul 08 '21 at 09:31