[SOLVED]Qvector Problem instansiate Thanks ALL
-
I got a little confusing problem with one of my Vectors.
In my headerfile I set it up like this.
@QVector <ClPresetLoadsTestValues> presetVector;
@later when I want to use it i try to add some Object with append like this
@if (presetVector.isEmpty())
qDebug() << "Empty";
else
qDebug() << "SIZE " << presetVector.size() ;presetVector.append(p);
@
but my program crashes
I have tried to do like this also
@QVector <ClPresetLoadsTestValues>* presetVector;
@and then i my constructor using new but with the same result;
anyone??? -
At what point your program actually crashes? When you create the QVector or whent you try to append to it?
-
on isEmpty()
-
That needs more code to evaluate. From what I see, it is correct. In which function do you call isEmpty? Where do you create it? where is the constructor, ...?
-
Do other containers or QVector with other type cause crash?
-
My h.file
@#ifndef GLOBALPARAMS_H
#define GLOBALPARAMS_H
#include <QVector>
#include <QList>
#include <QTime>
#include <clpresetloadstestvalues.h>class GlobalParams
{
public:
GlobalParams();
~GlobalParams();
enum Tester {Starttest, Loadtest, Batterytest, ParallellToMains, MainsFailureTEst, PresetLoadsTest , Notest};void setTest(int); int getTest(); void testRunning(bool ); bool getTestRunning(); void setNBState(int s); void setGBState(int s); void setRunningState(int s); void setRunningMode(int s); int getNBState(); int getGBState(); int getRunningState(); int getRunningMode(); void addValuesToLoadsTestTemp(int,int,int); double getRandomNumbers(float max, float min); QString roundnumbers(double dNumber);
private:
int testTyp;int NBState; int GBState; int RunningState; int RunningMode; bool TestRunning; QVector <ClPresetLoadsTestValues> presetVector;
};
@in my cpp file
@void GlobalParams::addValuesToLoadsTestTemp(int kw, int min, int sec)
{
ClPresetLoadsTestValues p;
p.setKW(kw);
p.setMin(min);
p.setSec(sec);if (presetVector.isEmpty()) qDebug() << "Empty"; else qDebug() << "SIZE " << presetVector.size() ; presetVector.append(p); qDebug() << "appendad";
}
@
My h.file to the object I want to save
@class ClPresetLoadsTestValues
{
public:
ClPresetLoadsTestValues();
ClPresetLoadsTestValues(int k, int m, int s);
~ClPresetLoadsTestValues();//set
void setKW(int k);
void setMin(int k);
void setSec(int k);//get
int getKw();
int getMin();
int getSec();private:
int kw;
int min;
int sec;};
@
I tried with a Struct also but same crash -
I'm using a vector in a different place in my application and there is no problem to use.
-
Check for that: you declare your presetVector like a value, so that constructor of it is called before the constructor of GlobalParams. So may be the object of GlobalParams haven't been instantiated yet?
-
I have tried to do like this also.
@QVector <.theClass> *myVector;
@
and in the constructor of globalparams@myVector = new QVector<theClass>();
@
but same crash ; -
Ok, once again, check if constructor of GlobalParams is called, add there qDebug()<<"anything";
-
It's the first thing I do in my program.
I got a lot of parameters in that object that I use in other places so thats no problem.
-
Please provide a small, complete, compilable and runnable sample program. We can have a look if and where it crashes ourselves then.
-
Now I'm back to my Work and will send a small application with this Vector problem.
I have put some QDebug lines in the code to:But where could I upload the program
-
I think that any file sharing hosting will be suitable. For example try http://rghost.net/
-
look for a file hoster (e.g. gitorious.org, but you need a logon) or any other
-
I have discovered a problem know that is a little confusing.
When I try to append a Object or a struct thats not in the class where the vector is the program crashes but when I put a struct inside the class and append this to the vector it works fine
-
Here is not the TAR file to test
-
Your code works without any crashes. I didn't do any changes though.
-
Try again with this code
I had comment the problem spots in the last code.
The new one is here
"Vectorproblems":http://rghost.net/31386231pls
-
toho71 either you have to read more about C++ or you did horrible typo. Here it is:
@myClass::~myClass()
{
delete this;
}@