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. Why i can not create an object ?
Forum Updated to NodeBB v4.3 + New Features

Why i can not create an object ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 737 Views 1 Watching
  • 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by Chris Kawa
    #2

    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 reaches main().

    Array ar=*new Array(text.toInt()); is another global variable, also initialized before your app reaches main(). The value of text here is an empty string, just as you declared it above. Empty string is not a number, so toInt() 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 and text 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?

    J 1 Reply Last reply
    2
    • Chris KawaC Chris Kawa

      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 reaches main().

      Array ar=*new Array(text.toInt()); is another global variable, also initialized before your app reaches main(). The value of text here is an empty string, just as you declared it above. Empty string is not a number, so toInt() 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 and text 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?

      J Offline
      J Offline
      JoeGreen
      wrote on last edited by
      #3

      @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 KawaC 1 Reply Last reply
      0
      • J JoeGreen

        @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 KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #4

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

        J 1 Reply Last reply
        2
        • Chris KawaC Chris Kawa

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

          J Offline
          J Offline
          JoeGreen
          wrote on last edited by
          #5

          @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 KawaC 1 Reply Last reply
          0
          • J JoeGreen

            @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 KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #6

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

            J 1 Reply Last reply
            1
            • Chris KawaC Chris Kawa

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

              J Offline
              J Offline
              JoeGreen
              wrote on last edited by JoeGreen
              #7

              @Chris-Kawa

              #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

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #8

                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 variable ar on the stack. This new Array creates second, unnamed variable on the heap. This =* copies the heap variable to the stack variable. This pointer=&ar takes address of a local variable ar and assigns it to a global variable. This } ends the scope. Local variable ar is destroyed. Unnamed heap variable is leaking memory and pointer points to garbage, because the local object no longer exists.

                First make pointer a member of class MainWindow, like I said, and initialize it to nullptr 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.

                J 1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  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 variable ar on the stack. This new Array creates second, unnamed variable on the heap. This =* copies the heap variable to the stack variable. This pointer=&ar takes address of a local variable ar and assigns it to a global variable. This } ends the scope. Local variable ar is destroyed. Unnamed heap variable is leaking memory and pointer points to garbage, because the local object no longer exists.

                  First make pointer a member of class MainWindow, like I said, and initialize it to nullptr 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.

                  J Offline
                  J Offline
                  JoeGreen
                  wrote on last edited by JoeGreen
                  #9

                  @Chris-Kawa
                  sorry but this line pointer = new Array(text.toInt());
                  gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *'

                  Chris KawaC W 2 Replies Last reply
                  0
                  • J JoeGreen

                    @Chris-Kawa
                    sorry but this line pointer = new Array(text.toInt());
                    gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *'

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @JoeGreen What type did you give to pointer and where did you declare it?

                    J 1 Reply Last reply
                    1
                    • J JoeGreen

                      @Chris-Kawa
                      sorry but this line pointer = new Array(text.toInt());
                      gives me this error: Incompatible pointer types assigning to 'int *' from 'Array *'

                      W Offline
                      W Offline
                      wrosecrans
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      1
                      • Chris KawaC Chris Kawa

                        @JoeGreen What type did you give to pointer and where did you declare it?

                        J Offline
                        J Offline
                        JoeGreen
                        wrote on last edited by JoeGreen
                        #12

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

                        thank you for your precious time

                        1 Reply Last reply
                        0

                        • Login

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