I am doing one project in which I define a data types like below
typedef QVector<double> QFilterDataMap1D;
typedef QMap<double, QFilterDataMap1D> QFilterDataMap2D;
Then there is one class with the name of mono_data in which i have define this variable
QFilterMap2D valid_filters;
mono_data Scan_data // Class
Now i am reading one variable from a .mat file and trying to save it in to above "valid_filters" QMap.
Qt Code: Switch view
for(int i=0;i<1;i++)
{
for(int j=0;j<1;j++)
{
Scan_Data.valid_filters[i][j]=valid_filters[i][j];
printf("\nValid_filters=%f",Scan_Data.valid_filters[i][j]);
}
}
The transferring is done successfully but then it gives run-time error
Windows has triggered a breakpoint in SpectralDataCollector.exe.
This may be due to a corruption of the heap, and indicates a bug in SpectralDataCollector.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information
Can anyone help in solving this problem. It will be of great help to me.
Thanks
Different issues here:
1. Using double as key type for a QMap
Using a
QMap<double, Foo>
is a very bad idea. the reason is that this is a container that let you access aFoo
given adouble
. For instance:This is problematic, because then, to retrieve the data contained in
map[key]
, you have to test ifkey
is either equal, smaller or greater than other keys in the maps. In your case, the key is adouble
, and testing if twodouble
s are equals is not a "safe" operation.2. Using an int as key while you defined it was double
Here:
i
is an integer, and you said it should be a double.3. Your loop only test for (i,j) = (0,0)
Are you aware that
is equivalent to:
?
4. Accessing a vector with operator[] is not safe
When you do:
You in fact do:
The first one is safe, and create the entry if it doesn't exist. The second one is not safe, the jth element in you vector must already exist otherwise it would crash.
Solution
It seems you in fact want a 2D array of double (i.e., a matrix). To do this, use:
Then, when you want to transfer one in another, simply use:
Or if you want to do it yourself:
If you want a 3D matrix, you would use: