Can not pass structure to a secound cpp file



  • Hi, I'm designing a programm which controlls my RGB strips over Wifi with MQTT.
    So I wrote this QT programm to set the color:
    It has 3 sliders btw.

    This is my colpic.cpp (the Widget file)

    #include "colpic.h"
    #include "ui_colpic.h"
    #include <sstream>
    #include <unistd.h>
    #include <mosquitto.h>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    
    using namespace std;
    const char* in;
    int num=0;
    string trig;
    ostringstream ostr;
    string str;
    
    int redval=0;
    int blueval=0;
    int greenval=0;
    
    
    
    colpic::colpic(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::colpic)
    {
    
        ui->setupUi(this);
    
    }
    
    colpic::~colpic()
    {
        delete ui;
    }
    
    
    void colpic::on_slideRED_sliderMoved(int position)
    {
        cout<<"RED: "<<(position)<<endl;
        redval=position;
        ostr <<redval;
        str = ostr.str();
        in= str.c_str();
    
    
        mosquitto_publish(mosq, NULL, "RGBRED", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    
    void colpic::on_slideGREEN_sliderMoved(int position)
    {
        cout<<"GREEN: "<<(position)<<endl;
        greenval=position;
        ostr <<blueval;
        str = ostr.str();
        in= str.c_str();
    
    
        mosquitto_publish(mosq, NULL, "RGBGREEN", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    
    void colpic::on_slideBLUE_sliderMoved(int position)
    {
        cout<<"BLUE: "<<(position)<<endl;
        blueval=position;
        ostr <<blueval;
        str = ostr.str();
        in= str.c_str();
    
    
    
        mosquitto_publish(mosq, NULL, "RGBBLUE", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    

    and this is my main.cpp

    #include "colpic.h"
    #include <QApplication>
    #include <sstream>
    #include <unistd.h>
    #include <mosquitto.h>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    void on_disconnect(struct mosquitto *mosq, void *userdata, int mid){
    
        mosquitto_connect(mosq, "192.168.1.31", 8883,0);
    }
    
    int main(int argc, char *argv[])
    {
        struct mosquitto *mosq;
        mosq = mosquitto_new("id", true, NULL);
        mosquitto_disconnect_callback_set(mosq, on_disconnect);
        mosquitto_connect(mosq, "192.168.1.31", 8883,0);
        QApplication a(argc, argv);
        colpic w;
        w.show();
    
    
        return a.exec();
    }
    
    

    I think I messed up the main.cpp. My problem is that I can not use the

    mosq
    

    structure in my colpic.cpp

    How do I need to pass it through?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can add a setter to colpic passing the pointer to your instance as parameter.

    Or since you have everything in colpic, why not create that structure instance in the constructor of colpic ?



  • @SGaist said in Can not pass structure to a secound cpp file:

    Or since you have everything in colpic, why not create that structure instance in the constructor of colpic ?

    Thank you for the quick answer, It was a good idea to connect with you all via the forum.

    So I changed my colpic constructur like this:

    colpic::colpic(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::colpic)
    {
    
        ui->setupUi(this);
        struct mosquitto *mosq;
        mosq = mosquitto_new("id", true, NULL);
        mosquitto_disconnect_callback_set(mosq, on_disconnect);
        mosquitto_connect(mosq, "192.168.1.31", 8883,0);
    
    }
    
    colpic::~colpic()
    {
        delete ui;
    }
    
    
    

    Actually the compiler errors are minimized to just 3 which are the same:
    mosq was not declared in this scope.
    It refers to the 3 callback functions below:

    void on_slideRED_sliderMoved(int position);
    void on_slideGREEN_sliderMoved(int position);
    void on_slideBLUE_sliderMoved(int position);
    

    So I think

    struct mosquitto *mosq;
    

    Is just working in the constructor of colpic .cpp.

    Do I need to set a setter there? And can you give me a hint how?
    Happy Christmas
    Joshua


  • Lifetime Qt Champion

    Make it a class member otherwise will only be available in the constructor and you will also be leaking memory since you won't be able to delete it when you're done.



  • @SGaist So I think I'm stuck in my brain in a very wrong path. So I added it to the colpic.h

    #ifndef COLPIC_H
    #define COLPIC_H
    
    #include <QWidget>
    
    namespace Ui {
    class colpic;
    
    }
    
    class colpic : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit colpic(QWidget *parent = 0);
        struct mosquitto *mosq;
    
        ~colpic();
    
    private slots:
    
    
        void on_slideRED_sliderMoved(int position);
    
        void on_slideGREEN_sliderMoved(int position);
    
        void on_slideBLUE_sliderMoved(int position);
    
    private:
        Ui::colpic *ui;
    };
    
    #endif // COLPIC_H
    
    

    Is that right? I think I'm missing an public constructor but I don't know how to write this.


  • Lifetime Qt Champion

    Remove struct mosquitto *mosq; from your constructor.

    Also, no need to make it public. It's only used within your class.



  • @SGaist So my colpic .cpp looks like:

    #include "colpic.h"
    #include "ui_colpic.h"
    #include <sstream>
    #include <unistd.h>
    #include <mosquitto.h>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    
    using namespace std;
    const char* in;
    int num=0;
    string trig;
    ostringstream ostr;
    string str;
    
    int redval=0;
    int blueval=0;
    int greenval=0;
    
    void on_disconnect(struct mosquitto *mosq, void *userdata, int mid){
    
        mosquitto_connect(mosq, "192.168.1.31", 8883,0);
    }
    
    
    colpic::colpic(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::colpic)
    {
    
        ui->setupUi(this);
        //struct mosquitto *mosq;
        mosq = mosquitto_new("id", true, NULL);
        mosquitto_disconnect_callback_set(mosq, on_disconnect);
        mosquitto_connect(mosq, "192.168.1.31", 8883,0);
    
    }
    
    colpic::~colpic()
    {
        delete ui;
    }
    
    
    void colpic::on_slideRED_sliderMoved(int position)
    {
        cout<<"RED: "<<(position)<<endl;
        redval=position;
        ostr <<redval;
        str = ostr.str();
        in= str.c_str();
    
    
        mosquitto_publish(mosq, NULL, "RGBRED", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    
    void colpic::on_slideGREEN_sliderMoved(int position)
    {
        cout<<"GREEN: "<<(position)<<endl;
        greenval=position;
        ostr <<blueval;
        str = ostr.str();
        in= str.c_str();
    
    
        mosquitto_publish(mosq, NULL, "RGBGREEN", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    
    void colpic::on_slideBLUE_sliderMoved(int position)
    {
        cout<<"BLUE: "<<(position)<<endl;
        blueval=position;
        ostr <<blueval;
        str = ostr.str();
        in= str.c_str();
    
    
    
        mosquitto_publish(mosq, NULL, "RGBBLUE", 11, in, 0, false);
        mosquitto_loop(mosq, 0, 1);
        ostr.str(string());
    }
    

    And my colpic.h

    #ifndef COLPIC_H
    #define COLPIC_H
    
    #include <QWidget>
    #include <mosquitto.h>
    namespace Ui {
    class colpic;
    
    }
    
    class colpic : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit colpic(QWidget *parent = 0);
    
    
        ~colpic();
    
    private slots:
    
    
        void on_slideRED_sliderMoved(int position);
    
        void on_slideGREEN_sliderMoved(int position);
    
        void on_slideBLUE_sliderMoved(int position);
    
    private:
        Ui::colpic *ui;
        struct mosquitto *mosq;
    
    };
    
    #endif // COLPIC_H
    

    But the compiler says, there is something written wrong in my private area in colpic.h.

    How do I have to change struct mosquitto *mosq; ?


  • Qt Champions 2016

    Hi
    You should use/show the actual errors compiler gives.
    When you say
    "But the compiler says, there is something written wrong"
    we cannot really guess as no extra info
    But with the real error, we can.
    You can right click an error and choose copy, then paste here. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.