Why i can not create an object ?
-
#include "mainwindow.h" #include "ui_mainwindow.h" using namespace std; //this is the simple class array class Array{ public: int size; int length,index; Array(int s){ size=s; index=9; } }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } // i declared that here so that i could use it outside the method QString text=""; void MainWindow::on_lineEdit_returnPressed() // size { text = ui->lineEdit->text(); // here i gave it a value to use it outside the scope of this //method ui->lineEdit->clear(); } Array ar=*new Array(text.toInt()); // the problem is here help please void MainWindow::on_lineEdit_2_returnPressed() // insert { ui->label_5->setText(QString::number(ar.size)); // prints zero why?? ui->label_4->setText(QString::number(ar.index));// prints 9 cuz it's already initialized in //the class }
hi there i need help with this code here
Array ar=*new Array(text.toInt());
when i try to create an object of that simple class Array that requires size in it's constructor
the value that reahces the class is always equal to zero and i do not no why
in another words this lineui->label_5->setText(QString::number(ar.size));
always prints zero which is i think the default value of int size found in Array class
so why is does this happen please?it is the first time working with qt so it would be great to give an advice about this situation .
what is the best practice to make this thing work? -
@JoeGreen What type did you give to
pointer
and where did you declare it?@Chris-Kawa
i 've made it
i v read about pointers and member class declarations
i had to put my class array declarations in a separate header file so that i could
declare an array pointer in mainWindow.h and it worked finallythank you for your precious time
-
It has nothing to do with Qt. Just pure C++.
You've put
QString text="";
in the global scope (outside any class methods). This means it's a global object initialized before your program even reachesmain()
.Array ar=*new Array(text.toInt());
is another global variable, also initialized before your app reachesmain()
. The value oftext
here is an empty string, just as you declared it above. Empty string is not a number, sotoInt()
fails and returns 0.Then, after all global variables are initialized your program enters
main()
, that creates main window, shows it and starts an event loop. After some time user presses enter in that line edit and the value of text is now set to something else. This is happening waaaaaaaaaaay after the Array object was constructed andtext
variable has now nothing to do with it.In short - your Array object is initialized at the start of the app. The value of
text
is set a lot later in your program, so of course it will not affect an object that already exists.what is the best practice to make this thing work?
Depends on what you want to achieve. Do you need that Array object to exist from the start or when that enter is pressed? Do you want it to be resizable or not?
-
It has nothing to do with Qt. Just pure C++.
You've put
QString text="";
in the global scope (outside any class methods). This means it's a global object initialized before your program even reachesmain()
.Array ar=*new Array(text.toInt());
is another global variable, also initialized before your app reachesmain()
. The value oftext
here is an empty string, just as you declared it above. Empty string is not a number, sotoInt()
fails and returns 0.Then, after all global variables are initialized your program enters
main()
, that creates main window, shows it and starts an event loop. After some time user presses enter in that line edit and the value of text is now set to something else. This is happening waaaaaaaaaaay after the Array object was constructed andtext
variable has now nothing to do with it.In short - your Array object is initialized at the start of the app. The value of
text
is set a lot later in your program, so of course it will not affect an object that already exists.what is the best practice to make this thing work?
Depends on what you want to achieve. Do you need that Array object to exist from the start or when that enter is pressed? Do you want it to be resizable or not?
@Chris-Kawa
thank you for replying,
i need the array object to be created after enter is pressed
in another words after the user enter the size of the array
and i do not want it to be resizable -
@Chris-Kawa
thank you for replying,
i need the array object to be created after enter is pressed
in another words after the user enter the size of the array
and i do not want it to be resizable@JoeGreen said in Why i can not create an object ?:
i need the array object to be created after enter is pressed
Ok, then create it in the function that is called when user presses enter and not in the global scope, which is executed at the start of the app.
-
@JoeGreen said in Why i can not create an object ?:
i need the array object to be created after enter is pressed
Ok, then create it in the function that is called when user presses enter and not in the global scope, which is executed at the start of the app.
@Chris-Kawa
if i done that i wont be able to use the local ar object outside of that method
and i do want to use in further methods -
@Chris-Kawa
if i done that i wont be able to use the local ar object outside of that method
and i do want to use in further methods@JoeGreen Make the pointer member of your class. Create an object in the method and assign it to that member, then you can use it in any other method of your class.
-
@JoeGreen Make the pointer member of your class. Create an object in the method and assign it to that member, then you can use it in any other method of your class.
#include "mainwindow.h" #include "ui_mainwindow.h" using namespace std; class Array{ public: int size; int length,index; Array(int s){ size=s; index=9; } }; Array *pointer; // the pointer member of class array MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_lineEdit_returnPressed() // size { QString text = ui->lineEdit->text(); ui->lineEdit->clear(); Array ar=*new Array(text.toInt()); pointer=&ar; // getting the address of the created object }//these methods are invoked when the app reaches main void MainWindow::on_lineEdit_2_returnPressed() // insert { ui->label_5->setText(QString::number(pointer->size));// garbage ui->label_4->setText(QString::number(pointer->index));// garbage }
i ve tried that but now pointer->size and pointer->index gives me garbage value
-
You've got pretty much all of it wrong.
Array *pointer;
That is not a class member. It's an uninitialized global variable. Member variables are declared in the header file, inside your class declaration.
This
Array ar
creates a local variablear
on the stack. Thisnew Array
creates second, unnamed variable on the heap. This=*
copies the heap variable to the stack variable. Thispointer=&ar
takes address of a local variablear
and assigns it to a global variable. This}
ends the scope. Local variablear
is destroyed. Unnamed heap variable is leaking memory andpointer
points to garbage, because the local object no longer exists.First make
pointer
a member of class MainWindow, like I said, and initialize it tonullptr
value. Then in your method simply do:pointer = new Array(text.toInt());
which creates a variable on the heap and assigns its address to the member.
Also remember that you have to delete that object somewhere and that this method can be called multiple times, so before you assign new value to
pointer
check if it already points to the previous object and if yes then delete it first. -
You've got pretty much all of it wrong.
Array *pointer;
That is not a class member. It's an uninitialized global variable. Member variables are declared in the header file, inside your class declaration.
This
Array ar
creates a local variablear
on the stack. Thisnew Array
creates second, unnamed variable on the heap. This=*
copies the heap variable to the stack variable. Thispointer=&ar
takes address of a local variablear
and assigns it to a global variable. This}
ends the scope. Local variablear
is destroyed. Unnamed heap variable is leaking memory andpointer
points to garbage, because the local object no longer exists.First make
pointer
a member of class MainWindow, like I said, and initialize it tonullptr
value. Then in your method simply do:pointer = new Array(text.toInt());
which creates a variable on the heap and assigns its address to the member.
Also remember that you have to delete that object somewhere and that this method can be called multiple times, so before you assign new value to
pointer
check if it already points to the previous object and if yes then delete it first.@Chris-Kawa
sorry but this linepointer = new Array(text.toInt());
gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *' -
@Chris-Kawa
sorry but this linepointer = new Array(text.toInt());
gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *'@JoeGreen What type did you give to
pointer
and where did you declare it? -
@Chris-Kawa
sorry but this linepointer = new Array(text.toInt());
gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *'@JoeGreen You may want to focus on following some basic C++ tutorials and learn the core language before you focus on learning Qt and making a complicated GUI. All of the Qt documentation and tutorials is going to assume a solid foundation of the basics of the language, like types and pointers.
-
@JoeGreen What type did you give to
pointer
and where did you declare it?@Chris-Kawa
i 've made it
i v read about pointers and member class declarations
i had to put my class array declarations in a separate header file so that i could
declare an array pointer in mainWindow.h and it worked finallythank you for your precious time