PDA

View Full Version : no match for 'operator<<' (operand types are 'QDebug' and 'const QSerialPortInfo')



Neeseta
20th April 2017, 19:01
All of the following is on a Raspberry Pi 3 with Qt 5.3.2 (GCC 4.9.2, 32 bit).

MainWindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSerialPort/qserialportinfo.h>
#include <QtSerialPort/qserialport.h>
#include <QObject>
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QList>



namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);


bool zaber_is_available;
~MainWindow();

private slots:



void on_Calibration_25mm_stage_move_button_clicked(int position);

void on_Calibration_25mm_stage_home_button_clicked();

void on_Calibration_25mm_stage_out_button_clicked();

void on_Calibration_300mm_stage_move_button_clicked(int position);

void on_Calibration_300mm_stage_home_button_clicked();

void on_Shot_tab_25mm_stage_move_button_clicked(int position);

void on_Shot_tab_25mm_stage_home_button_clicked();

void on_Shot_tab_25mm_stage_out_button_clicked();

void CommZaber(QString);

void on_Calibration_25mm_slider_sliderMoved(int position);

void on_Calibration_300mm_slider_sliderMoved(int position);

void on_Shot_tab_25mm_slider_sliderMoved(int position);

void on_Shot_tab_25mm_spinbox_valueChanged(int arg1);

void on_Calibration_300mm_spinbox_valueChanged(int arg1);

void on_Calibration_25mm_spinbox_valueChanged(int arg1);


private:
Ui::MainWindow *ui;
QSerialPortInfo *serialPortInfo;
QSerialPort *zaber;


static const quint16 zaber_vendor_id = 0403;
static const quint16 zaber_product_id = 6001;

};

#endif // MAINWINDOW_H


main.cpp


#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QSpinBox>
#include <QSlider>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("Compact 6 Radiometer Control Unit");
w.show();






return a.exec();
}


MainWindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/qserialport.h>
#include <QtSerialPort/qserialportinfo.h>
#include <QObject>
#include <QList>
#include <QDebug>
#include <QString>
#include <QWidget>
#include "qmath.h"
#include <QIODevice>

QT_USE_NAMESPACE

char Reply_Bytes[6] ={0};
qint64 maxSize = 6;
int value[4];
int value2[4];
char position_command[6] = {0};
double new_value;
float conversion;
float distance;
const double global_x = pow(256,3);
const double global_y = pow(256,2);
const double global_z = pow(256,4);
float input_position;
int Cmd_Byte_6 = 0;
int Cmd_Byte_5 = 0;
int Cmd_Byte_4 = 0;
int Cmd_Byte_3 = 0;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

zaber_is_available = false;

qDebug() <<"Number of ports: "<<QSerialPortInfo::availablePorts();

foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
qDebug()<< "Name: " << serialPortInfo.portName();
qDebug()<< "Description: "<< serialPortInfo.description();
qDebug()<< "Manufacturer: " << serialPortInfo.manufacturer();
if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier())
{
qDebug() << "Has VendorID and ProductID:" << serialPortInfo.hasVendorIdentifier() <<
"and" << serialPortInfo.hasVendorIdentifier();
if(serialPortInfo.vendorIdentifier() == zaber_vendor_id)
{
qDebug() << "Vendor: " << serialPortInfo.vendorIdentifier();
if(serialPortInfo.productIdentifier() == zaber_product_id)
{
qDebug() << "Product: " << serialPortInfo.productIdentifier();
zaber = new QSerialPort;
zaber_is_available = true;
}
}
}
}

//code removed as QDebug does not show in it and it is too long.

void MainWindow::CommZaber(QString command)
{
if(zaber->isWritable()){
zaber->write(command.toStdString().c_str());
}
else{
qDebug() << "Couldn't Write to Serial";
}

}


The error I get is as stated in the post title. The pointer for the error leads me to the library, as if the error was in the library.

I have tried adding the following to my header file to fix the issue but it did not work.


class DebugClass : public QObject
{
Q_OBJECT
public:
explicit DebugClass(QObject *parent = 0);
friend QDebug operator<< (QDebug dbg, const DebugClass &info){
dbg.nospace() << "This is x: " <<info.x;
return dbg.maybeSpace();
}

private:
int x;
};


I have gotten this far by the good graces of Google. Any help would be greatly appreciated.

d_stranz
20th April 2017, 22:21
How will defining operator<< for a totally unrelated class help solve the problem? The compiler is complaining because QSerialPortInfo::availablePorts() returns QList<QSerialPortInfo> and when you try to output this list through qDebug(), it can't find a match for "QDebug operator<<( QDebug, const QSerialPortInfo & )".

When QDebug tries to serialize QList<T> (where in your case, T == QSerialPortInfo), it will take each member of the list and apply operator<<( QDebug, const T & ) to that member. This operator doesn't exist in the Qt library, so you'll have to write it. You have basically done that inside your foreach() loop.

On the other hand, if all you are asking for in the first qDebug() statement in line 40 is the count of the available ports, then add ".size()" to the end of "QSerialPortInfo::availablePorts()".