[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_OBJECTpublic:
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_Hcpp 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;
} -
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.
-
Firstly, thanks for quick response. You often do it...
I can't understand. Can you explain with code?
-
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' -
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;
} -
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.
-
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_OBJECTpublic:
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);
};
... -
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..
-
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 :)