سؤال

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 a Foo given a double. For instance:

map[0.45] = foo1;
map[15.74] = foo2;

This is problematic, because then, to retrieve the data contained in map[key], you have to test if key is either equal, smaller or greater than other keys in the maps. In your case, the key is a double, and testing if two doubles are equals is not a "safe" operation.

2. Using an int as key while you defined it was double

Here:

Scan_Data.valid_filters[i][j]=valid_filters[i][j];

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

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]);
    }
}

is equivalent to:

Scan_Data.valid_filters[0][0]=valid_filters[0][0];
printf("\nValid_filters=%f",Scan_Data.valid_filters[0][0]);

?

4. Accessing a vector with operator[] is not safe

When you do:

Scan_Data.valid_filters[i][j]

You in fact do:

QFilterDataMap1D & v = Scan_Data.valid_filters[i]; // call QMap::operator[](double)
double             d = v[j];                       // call QVector::operator[](int)

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:

typedef QVector<double> QFilterDataMap1D;
typedef QVector<QFilterDataMap1D> QFilterDataMap2D;

Then, when you want to transfer one in another, simply use:

Scan_Data.valid_filters = valid_filters;

Or if you want to do it yourself:

Scan_Data.valid_filters.clear();
for(int i=0;i<n;i++)
{
    Scan_Data.valid_filters << QFilterDataMap1D();
    for(int j=0;j<m;j++)
    {
        Scan_Data.valid_filters[i] << valid_filters[i][j];
        printf("\nValid_filters=%f",Scan_Data.valid_filters[i][j]);
    }
}

If you want a 3D matrix, you would use:

typedef QVector<QFilterDataMap2D> QFilterDataMap3D;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top