c++qtqt6msvc12

msvc compiler gives an error, fine with minGW


I have a simple Qt (v.6.4.2) code that works fine with MinGW64 compilator (Windows 10):

#include <QCoreApplication>
#include "fftw3.h"

int main()
{
    QVector<int> data = {0, 1, 0, 1, 0, 1, 0, 1}; // 2^3=8 values

    //-----Start fft----- (using fftw library)
    fftw_complex in[data.size()], out[data.size()];
    for (int i = 0; i < data.size(); i++) {
        in[i][0] = data[i];
        in[i][1] = 0;
    }
    fftw_plan p;
    p = fftw_plan_dft_1d(data.size(), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    //-----End fft-----

    double Amplitude;
    for (int i = 0; i < data.size(); i++) {
        Amplitude = sqrt( pow(out[i][0], 2) + pow(out[i][1], 2) );
        qDebug() << Amplitude;
    }
    return 0;
}

However compilation with msvc 64-bit compilator (v 17.4.33213.308) comes out with errors:

error C2131: expression is not defined by a constant
note: the crash was caused by an undefined function or a function not declared to "constexpr"
note: see usage of "QList<int>::size"
error C2131: expression is not defined by a constant
note: the crash was caused by an undefined function or a function not declared to "constexpr"
note: see usage of "QList<int>::size"
error C3863: array type "fftw_complex['function']" is non-assignable
error C3863: array type "fftw_complex['function']" is non-assignable
error C2668: pow: ambiguous call to overloaded function

Can I somehow solve this problem, or using fftw library with msvc compilator is impossible? Other Qt projects without fftw library work fine. Project *pro file:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp

LIBS += D:\QtProjects\fft\lib\libfftw3-3.lib
INCLUDEPATH += D:\QtProjects\fft\lib

Solution

  • You're attempting to use a GNU compiler extension (Variable-Length Arrays) which allows you to define an arrays bounds with a dynamic value. This is not standard C++ and is not supported in MSVC.

    The lines in question are here:

    QVector<int> data = {0, 1, 0, 1, 0, 1, 0, 1}; // 2^3=8 values
    
    //-----Start fft----- (using fftw library)
    fftw_complex in[data.size()], out[data.size()];
    

    When defining the bounds of an array the value must be a compile time constant.

    Try changing the above lines to the following:

    QVector<int> data = {0, 1, 0, 1, 0, 1, 0, 1}; // 2^3=8 values
    
    //-----Start fft----- (using fftw library)
    fftw_complex in[8], out[8];
    

    If the bounds of the array aren't known until runtime you'll have to use something like a std::vector instead.