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. Class
Forum Updated to NodeBB v4.3 + New Features

Class

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 1.5k Views 2 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.
  • privatepepperP Offline
    privatepepperP Offline
    privatepepper
    wrote on last edited by privatepepper
    #1

    Hello,
    why I can not add any more variables to my class? If I add a new variable to the class, my program crashes.

    #ifndef GRAPH_H
    #define GRAPH_H
    
    #include <QtCore>
    #include <QtGui>
    
    
    class Graph
    {
    public:
    
        Graph();
    
        int width = 40;
        int height = 30;
    
        int Vertices;
    
        void initialize_matrix(int nodes){
            Vertices = nodes;
            matrix->clear();
            store_searching_path.clear();
            store_path.clear();
            matrix = new QVector <int> [nodes];
        }
    
        void addEdge(int vertice1, int vertice2){
            matrix[vertice1].push_back(vertice2);
        }
    
        QVector <int> store_searching_path;
        QVector <int> store_path;
    
        bool BFS(int start, int end);
        bool DFS(int start, int end);
        //void recursive_DFS(int source);
        void dijkstra_algorithm();
    
    private:
    
        QVector <int> *matrix;
        //bool *visited;
        int blabla;
    
    };
    
    #endif // GRAPH_H
    

    For example when I add blabla integer my program crashes, if I delete blabla integer then everything is fine

    Pl45m4P 1 Reply Last reply
    0
    • privatepepperP privatepepper

      Hello,
      why I can not add any more variables to my class? If I add a new variable to the class, my program crashes.

      #ifndef GRAPH_H
      #define GRAPH_H
      
      #include <QtCore>
      #include <QtGui>
      
      
      class Graph
      {
      public:
      
          Graph();
      
          int width = 40;
          int height = 30;
      
          int Vertices;
      
          void initialize_matrix(int nodes){
              Vertices = nodes;
              matrix->clear();
              store_searching_path.clear();
              store_path.clear();
              matrix = new QVector <int> [nodes];
          }
      
          void addEdge(int vertice1, int vertice2){
              matrix[vertice1].push_back(vertice2);
          }
      
          QVector <int> store_searching_path;
          QVector <int> store_path;
      
          bool BFS(int start, int end);
          bool DFS(int start, int end);
          //void recursive_DFS(int source);
          void dijkstra_algorithm();
      
      private:
      
          QVector <int> *matrix;
          //bool *visited;
          int blabla;
      
      };
      
      #endif // GRAPH_H
      

      For example when I add blabla integer my program crashes, if I delete blabla integer then everything is fine

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @privatepepper

      Your blabla int member itself is most likely not the reason for your crash. Where do you use it?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      privatepepperP 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @privatepepper

        Your blabla int member itself is most likely not the reason for your crash. Where do you use it?

        privatepepperP Offline
        privatepepperP Offline
        privatepepper
        wrote on last edited by
        #3

        @Pl45m4
        That's the point, I don't use it, I added blabla for the example

        1 Reply Last reply
        0
        • Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          Where exactly crashes your program? Any error message?

          @privatepepper said in Class:

          int width = 40;
          int height = 30;
          
          int Vertices;
          

          What's the reason for them being public?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          privatepepperP 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            Where exactly crashes your program? Any error message?

            @privatepepper said in Class:

            int width = 40;
            int height = 30;
            
            int Vertices;
            

            What's the reason for them being public?

            privatepepperP Offline
            privatepepperP Offline
            privatepepper
            wrote on last edited by
            #5

            @Pl45m4

            I get only one error - The program has unexpectedly finished.

            I use width and height in the mainwindow class to draw grid of QGraphicsRectItems , and I just realized that Vertices should be private, because I use Vertices only in this class, but now when i add Vertices to private and delete blabla integer my program too crashes;/

            Pl45m4P 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Then use the debugger to see what is happening.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • privatepepperP privatepepper

                @Pl45m4

                I get only one error - The program has unexpectedly finished.

                I use width and height in the mainwindow class to draw grid of QGraphicsRectItems , and I just realized that Vertices should be private, because I use Vertices only in this class, but now when i add Vertices to private and delete blabla integer my program too crashes;/

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @privatepepper said in Class:

                I use width and height in the mainwindow class to draw grid of QGraphicsRectItems

                There are fancy things called setters / getters :-)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                privatepepperP 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @privatepepper said in Class:

                  I use width and height in the mainwindow class to draw grid of QGraphicsRectItems

                  There are fancy things called setters / getters :-)

                  privatepepperP Offline
                  privatepepperP Offline
                  privatepepper
                  wrote on last edited by
                  #8

                  @Pl45m4
                  what do you mean by that?

                  I found a way to fix my code. But I don't know why this thing works and what is that. When I started to use the debugger on my ide opened like another file, it looked like behind the scenes what qt is doing.
                  Little part of that code:

                  inline void QVector<T>::clear()
                  {
                      //if (!d->size)
                      //    return;
                     // destruct(begin(), end());
                     // d->size = 0;
                  }
                  

                  And if i comment out these lines, my code works fine and i can add new variables to my class:). Could someone explain what is this and why my code didn't work before?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    As I already suggested, use the debugger.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    privatepepperP 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      As I already suggested, use the debugger.

                      privatepepperP Offline
                      privatepepperP Offline
                      privatepepper
                      wrote on last edited by
                      #10

                      @SGaist
                      Yeah I know, and thanks for the help:), I already fixed my code as I mentioned above, but I don't understand what I fixed:/

                      JonBJ 1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        If you are talking about the QVector snippet, you did not fix anything, you broke QVector.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • privatepepperP privatepepper

                          @SGaist
                          Yeah I know, and thanks for the help:), I already fixed my code as I mentioned above, but I don't understand what I fixed:/

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @privatepepper
                          Your comment-out code prevents QVector<T>::clear() from actually clearing its elements, and then your code works. I would look at everywhere you use a QVector. You have matrix->clear();; just what do you do with your QVector <int> *matrix? It is a member variable right next to where you introduce a new variable and then it crashes....

                                  matrix->clear();
                                  ...
                                  matrix = new QVector <int> [nodes];
                          

                          What about first time round? You do initialize matrix before this, don't you, else you're trying to clear() a random area of memory....

                          privatepepperP 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @privatepepper
                            Your comment-out code prevents QVector<T>::clear() from actually clearing its elements, and then your code works. I would look at everywhere you use a QVector. You have matrix->clear();; just what do you do with your QVector <int> *matrix? It is a member variable right next to where you introduce a new variable and then it crashes....

                                    matrix->clear();
                                    ...
                                    matrix = new QVector <int> [nodes];
                            

                            What about first time round? You do initialize matrix before this, don't you, else you're trying to clear() a random area of memory....

                            privatepepperP Offline
                            privatepepperP Offline
                            privatepepper
                            wrote on last edited by
                            #13

                            @JonB
                            @SGaist

                            Thanks for help very much :), like JonB said the problem was matrix->clear(), I feel so dumb right now:D, lets hope that I will learn from this mistake; but it is weird for me that this problem only occurred when I tried to add new variables to the header file.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              There is also no need to use a pointer here.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              privatepepperP 1 Reply Last reply
                              1
                              • Christian EhrlicherC Christian Ehrlicher

                                There is also no need to use a pointer here.

                                privatepepperP Offline
                                privatepepperP Offline
                                privatepepper
                                wrote on last edited by privatepepper
                                #15

                                @Christian-Ehrlicher said in Class:

                                There is also no need to use a pointer here.

                                Do you mean QVector <int> *matrix ? I added this pointer because I want that this vector would be 2d

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Then use QVector<QVector<int>> matrix

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  privatepepperP 1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    Then use QVector<QVector<int>> matrix

                                    privatepepperP Offline
                                    privatepepperP Offline
                                    privatepepper
                                    wrote on last edited by
                                    #17

                                    @SGaist

                                    Okay, I will use QVector<QVector<int>> matrix, but why I shouldn't use a pointer? Is there a big difference? Sorry for dumb questions...

                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @privatepepper said in Class:

                                      but why I shouldn't use a pointer?

                                      Because it's not needed and when using an object you can't forget to initialize (and delete) it (as you did both)

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      1 Reply Last reply
                                      4

                                      • Login

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