[solved] Using function parameter Class object



  • Hello friends

    I have a basic problem. I can't do it. I have a MainWindow class and genYapi structure. I want to use func function. That function has a genYapi &gen parameter. I got errors. How can I use genYapi class object in func member function?

    Errors:

    • error: C2664: 'void MainWindow::func(genYapi &)' : cannot convert argument 1 from 'MainWindow::genYapi' to 'genYapi &'
    • error: C2664: 'void MainWindow::func(genYapi &)' : cannot convert argument 1 from 'MainWindow::genYapi [5]' to 'genYapi &'
    • error: C2511: 'void MainWindow::func(MainWindow::genYapi &)' : overloaded member function not found in 'MainWindow'

    ** header file**
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class genYapi;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    public slots:
    void func(genYapi &gen);

    private:
    Ui::MainWindow *ui;

    struct genYapi{
        double value;
    
        int basSayisi;
    }gen1[5];
    

    };
    #endif // MAINWINDOW_H

    cpp file
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>

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

    func(gen1);
    

    }
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::func(genYapi &gen){
    qDebug() << "gen.value: " << gen.value;
    }


  • Lifetime Qt Champion

    Hi,

    You are using a type that has not been declared yet in your func slot (which itself is declared before the genYapi). Also note that genYapi it's a private type and you are using it in a public slot.



  • @SGaist

    Firstly, thanks for quick response. You often do it...

    I can't understand. Can you explain with code?


  • Lifetime Qt Champion

    genYapi belongs to MainWindow so you have to rewrite the function signature like:

    void MainWindow::func(MainWindow::genYapi &gen){
    qDebug() << "gen.value: " << gen.value;
    }
    


  • @SGaist I did but I got same errors.

    error: C2664: 'void MainWindow::func(genYapi &)' : cannot convert argument 1 from 'MainWindow::genYapi' to 'genYapi &'
    error: C2664: 'void MainWindow::func(genYapi &)' : cannot convert argument 1 from 'MainWindow::genYapi [5]' to 'genYapi &'
    error: C2511: 'void MainWindow::func(MainWindow::genYapi &)' : overloaded member function not found in 'MainWindow'


  • Lifetime Qt Champion

    You have to declare it before you use it



  • @SGaist said:

    before you use

    Do you say like that?

    cpp file
    void MainWindow::func(MainWindow::genYapi &gen){
    qDebug() << "gen.value: " << gen.value;
    }

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

    MainWindow::~MainWindow()
    {
    delete ui;
    }


  • Lifetime Qt Champion

    No, you have to declare genYapi earlier. Again, note that the way you do it currently, no other classes than MainWindow will be able to use that struct.



  • @SGaist

    I change code and It works! Thank you a lot!

    I have a question again. This func function allows me that func(gen[0]) or func(gen[50]) etc. How can I use func(gen) ? I want to control all of array members at once. I look for solution on Internet, but I can't do it. Class Inheritance is complicated for me...

    header file
    ...
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;

    struct genYapi{
        double deger;
        bool elitMi=0;
        double amacFonkSnc;
    
        int basSayisi;
    }gen1[EN_FAZLA_BIREY], gen2[EN_FAZLA_BIREY], gen3[EN_FAZLA_BIREY];
    

    public slots:
    void func(MainWindow::genYapi &gen);
    };
    ...


  • Lifetime Qt Champion

    Pass a pointer to your struct and loop over your array. You can also consider using e.g. QVector.

    But again, func can't be called from outside MainWindow since genYapi is a private structure.



  • @SGaist Thank you so much brother. Have a good day..


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

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