Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can not pass structure to a secound cpp file
Forum Update on Monday, May 27th 2025

Can not pass structure to a secound cpp file

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sigrokBlack
    wrote on last edited by sigrokBlack
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        S Offline
        S Offline
        sigrokBlack
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            S Offline
            S Offline
            sigrokBlack
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Remove struct mosquitto *mosq; from your constructor.

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                Remove struct mosquitto *mosq; from your constructor.

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

                S Offline
                S Offline
                sigrokBlack
                wrote on last edited by sigrokBlack
                #7

                @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; ?

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  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. :)

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved