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. QVector
Forum Update on Monday, May 27th 2025

QVector

Scheduled Pinned Locked Moved General and Desktop
31 Posts 9 Posters 10.3k Views
  • 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.
  • P Offline
    P Offline
    pbhuter
    wrote on last edited by
    #1

    I wrote some code at work today, and got a QVector error when I tried to run the code. I was going to ask someone at work. Then I wrote some totally different code at home, and I got a QVector error (not 100% sure it's the same error). Anyone know what's going on? I'm using Qt 4.5 at work and 4.7.3 at home. I can post code, if need be.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Without knowing what the errors are, I don't think anyone can be of much help.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pbhuter
        wrote on last edited by
        #3

        Here's the qvector.h line it's crashing on:

        Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          a bit mor code would help, e.g. the code you have written and what you are doing there...
          seams that the index is wrong...

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pbhuter
            wrote on last edited by
            #5

            from reader.h:
            @
            {
            class Reader
            {
            protected:
            int readFile();

            public:
              Reader(const QString &filename;&#41;
              {
                _filename = filename;
                if (readFile(&#41;&#41;
                {
                  _filename = "";
                }
              };
              virtual ~Reader(&#41;
              {
              };
            
              virtual bool isValid();
            
              int getStages()
              {
                readFile&#40;&#41;;
                return _stages;
              };
            
              QVector<double> getWetMasses(&#41;
              {
                readFile(&#41;;
                return _wMasses;
              };
            

            QVector<double> getFuelMasses()
            {
            readFile();
            return _fMasses;
            };

            QVector<double> getDryMasses()
            {
            readFile();
            return _dMasses;
            };

            QVector<double> getFlowRates()
            {
            readFile();
            return _flowRates;
            };

            QVector<double> getBurnTimes()
            {
            readFile();
            return _burnTimes;
            };

            QVector<double> getThrusts()
            {
            readFile();
            return _thrusts;
            };

            QVector<double> getAlphas()
            {
            readFile();
            return _alphas;
            };

            double getRVMass()
            {
            readFile();
            return _RVMass;
            };

            double getRVAlpha()
            {
            readFile();
            return _RVAlpha;
            };

              QString filename(&#41;
              {
                return _filename;
              };
            
            private:
              QString _filename;
              int _stages;
              QVector<double> _wMasses;
            

            QVector<double> _fMasses;
            QVector<double> _dMasses;
            QVector<double> _flowRates;
            QVector<double> _burnTimes;
            QVector<double> _thrusts;
            QVector<double> _alphas;
            double _RVMass;
            double _RVAlpha;
            };
            }@

            from reader.cpp:

            @{
            int ret = 0;

            if (_filename.isEmpty())
            {
            return 1;
            }

            // create QFile, and open and initialize QTextStream
            QFile infile(_filename);
            if (!(infile.open(QIODevice::ReadOnly)))
            {
            return 1;
            }

            QTextStream its(&infile;);

            // parse header
            bool dataRow = false;

            QString line;

            while (!dataRow && !its.atEnd())
            {
            line = its.readLine();

            if (line.contains(QRegExp("\\d+\\.?\\d*"))) // check for data row
            {
              dataRow = true;
            }
            

            } //should have first row of data

            _stages = line.toInt();

            //read rest of file
            while (!its.atEnd())
            {
            int i = 0;
            while(i < _stages)
            {
            line = its.readLine();
            _wMasses.push_back(line.toDouble());

            line = its.readLine();
            _fMasses.push_back(line.toDouble());

            line = its.readLine();
            _dMasses.push_back(line.toDouble());

            line = its.readLine();
            _flowRates.push_back(line.toDouble());

            line = its.readLine();
            _burnTimes.push_back(line.toDouble());

            line = its.readLine();
            _thrusts.push_back(line.toDouble());

            line = its.readLine();
            _alphas.push_back(line.toDouble());

            i++;
            }

            line = its.readLine();
            _RVMass = line.toDouble();

            line = its.readLine();
            _RVAlpha = line.toDouble();
            }

            return ret;
            }@

            That's the code that I'm pretty sure it crashes on. In main.cpp I initialize the variables (as ints, or doubles, or QVector<double>s, but I'm adding stuff to the variables in the reader.cpp. The work code is the same, just with different variables.

            Thanks for any help.

            Edit : @ added by Eddy. Please use the code button on top of this edit field to make code more readable for others

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Run your app in the debugger, then when it breaks due to hitting this assertion click up in the call stack to your code to see which line is causing the problem and to investigate what value the index has and why.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #7

                A huge code paste and not a single usage of operator[]... please, debug your code and paste some RELEVANT sections.

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pbhuter
                  wrote on last edited by
                  #8

                  Well, now it doesn't crash. I didn't change any code from when I posted the initial post last night, so there's no reason for it to be working, now. But it does. Sort of. I'm now having a problem with QFile:

                  QFile infile(_filename) where _filename = test.dat (an ASCII text file).

                  infile shows up as {...} when I step through the code, so my input file never actually gets read. Anyone have an idea as to why? Or should I just wait 12 hours for VS2010/Qt to magically work for me...

                  1 Reply Last reply
                  0
                  • ZlatomirZ Offline
                    ZlatomirZ Offline
                    Zlatomir
                    wrote on last edited by
                    #9

                    You say you use VS2010, then have you built Qt framework with VS 2010?
                    //the Qt binaries built with VS2008 might give you weird run-time errors if the application is built with VS2010

                    https://forum.qt.io/category/41/romanian

                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      ZapB
                      wrote on last edited by
                      #10

                      There is no magic. You must have changed the build somehow (stale file perhaps). We cannot tell from your description what the problem is. Please provide a clear description of the problem and ideally a small compilable example that reproduces it.

                      Nokia Certified Qt Specialist
                      Interested in hearing about Qt related work

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pbhuter
                        wrote on last edited by
                        #11

                        Please disregard the additional problem from my last post. I'm actually having a problem with

                        QTextStream its(&infile;);

                        infile is loading the name of the input file, but QTextStream its isn't actually reading the file. I go on to do:

                        line = its.readLine(), but line always shows up as "" (even though the first line of the file is a 2).

                        Anyone able to help, I would appreciate it. Thanks in advance.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          pbhuter
                          wrote on last edited by
                          #12

                          CODE:

                          QFile infile(_filename);
                          /* if (!(infile.open(QIODevice::ReadOnly)))
                          {
                          return 1;
                          }*/

                          QTextStream its(&infile;);

                          // parse header
                          bool dataRow = false;

                          QString line;

                          while (!dataRow && !its.atEnd())
                          {
                          line = its.readLine();

                          if (line.contains(QRegExp("\\d+\\.?\\d*"))) // check for data row
                          {
                            dataRow = true;
                          }
                          

                          } //should have first row of data

                          _stages = line.toInt();

                          1 Reply Last reply
                          0
                          • EddyE Offline
                            EddyE Offline
                            Eddy
                            wrote on last edited by
                            #13

                            Please use the code tag (button on the right with <> symbol on it)
                            "Forum help":http://developer.qt.nokia.com/wiki/ForumHelp

                            Qt Certified Specialist
                            www.edalsolutions.be

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              pbhuter
                              wrote on last edited by
                              #14

                              @ QFile infile(_filename);
                              /* if (!(infile.open(QIODevice::ReadOnly)))
                              {
                              return 1;
                              }*/

                              QTextStream its(&infile;);

                              // parse header
                              bool dataRow = false;

                              QString line;

                              while (!dataRow && !its.atEnd())
                              {
                              line = its.readLine();

                              if (line.contains(QRegExp("\\d+\\.?\\d*"))) // check for data row
                              {
                                dataRow = true;
                              }
                              

                              } //should have first row of data

                              _stages = line.toInt();@

                              Sorry, I didn't notice it didn't post correctly.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mlong
                                wrote on last edited by
                                #15

                                You may want to open your file before trying to read from it.

                                Software Engineer
                                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  pbhuter
                                  wrote on last edited by
                                  #16

                                  Well, obviously - why didn't I see that. However, infile.open() doesn't work. I apparently need some arguments. I looked in the online Qt documentation for QFile, and I don't see anything about "open".

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mlong
                                    wrote on last edited by
                                    #17

                                    QFile inherits "open":http://doc.qt.nokia.com/4.7-snapshot/qiodevice.html#open from QIODevice. You want
                                    @
                                    infile.open(QIODevice::ReadOnly);
                                    @

                                    Software Engineer
                                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      pbhuter
                                      wrote on last edited by
                                      #18

                                      I did

                                      @infile.open(QIODevice::ReadOnly)@

                                      But I'm still having the same issue.

                                      1 Reply Last reply
                                      0
                                      • Z Offline
                                        Z Offline
                                        ZapB
                                        wrote on last edited by
                                        #19

                                        Please post a small compilable example that reproduces your problem. Then we can try it out ourselves and will likely be able to help you much easier.

                                        Nokia Certified Qt Specialist
                                        Interested in hearing about Qt related work

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          pbhuter
                                          wrote on last edited by
                                          #20

                                          @
                                          QFile infile(_filename);

                                          infile.open(QIODevice::ReadOnly);

                                          QTextStream its(&infile;);

                                          // parse header
                                          bool dataRow = false;

                                          QString line;

                                          while (!dataRow && !its.atEnd())
                                          {
                                          line = its.readLine();

                                          if (line.contains(QRegExp("\\d+\\.?\\d*"))) // check for data row
                                          {
                                            dataRow = true;
                                          }
                                          

                                          } //should have first row of data

                                          _stages = line.toInt();@

                                          I get into the while loop and it seems to think its is empty, so really I'm thinking it's still an issue with QTextStream. Let me know if you need anything else.

                                          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