Unsolved Cannot pass variable to another Dialog
-
I think you need a pointer, for example:
char s[5][2]; char (*s2)[2]=s; // pointer to array of two chars
Passing an array to a function is the same as passing a pointer to the first dimension:
void myFunct(char s[5][2]) // is the same as: void myFunct(char (*s)[2]) // more simply, you can omit the size of the first dimension: void myFunct(char s[][2])
-
@SGaist said in Cannot pass variable to another Dialog:
You are dereferencing your pointer three time, that's why it complains.
Out of curiosity, what do you have in that three-dimensional table ?
Three-dimensional array represents a 3D space in store.
My code:
Store store[5][5][10]; void MainWindow::setData(Store store_init[][5][10]) { store = store_init; }
error: incompatible types in assignment of 'Store (*)[5][10]' to 'Store [5][5][10]' store = store_init; ^
Maybe I will set data using three nested loops :D
-
@t0msk You cannot do it like this. C/C++ will not copy the content of the array if you assign it to another array. Arrays are basically pointers in C/C++. And [5][5][10] is not the same as [][5][10] (here the first dimension is not specified)!
You should take a look at std::array. -
I dont know how to copy content of whole array to another using 1 line, I solved it by loops:
int i, j, k; for(i = 0;i < 5;i++) { for(j = 0;j < 5;j++) { for(k = 0;k < 10;k++) { store[i][j][k] = store_init[i][j][k]; } } }
-
One more question.
Calling window:
int test = 7; Dialog *window = new Dialog(); window->setAttribute(Qt::WA_DeleteOnClose); window->setData(test); window->show();
Dialog.h
public: explicit Dialog(QWidget *parent = 0); ~Dialog(); void setData(int test);
Dialog.cpp
int second; Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->label->setText(QString::number(second)); } void Dialog::setData(int test) { second = test; } Dialog::~Dialog() { delete ui; }
Problem is that value in ui->label is 0 and not 7, why? :/
-
Because it's
test
that is 7 andsecond
is not initialised so it's pure luck that it's 0. -
And how to initialise "second"?
-
Assign it a value in the constructor initializer list.
-
void Dialog::setData(int test) { second = test; ui->label->setText(QString::number(second)); }
In the constructor, you can set the default value ( 0 ?)
-
@t0msk You could use the C function memcpy() - it is much more efficient compared to your solution and is only one line :-)