Compile Error C2131 and C3863 on Windows but NOT o

2020-05-09 17:33发布

I have a piece of code that compiles and works fine on Linux (Raspbian) but doesn't compile on windows (VS 17).

I am using CMAKE 3 for cross platform compiling, and like I said, I have no problem building this on Linux.

Here are the only CMAKE options that I am using:

cmake_minimum_required(VERSION 3.1)
project(Track)
set (CMAKE_CXX_STANDARD 11)
...
// The rest of the CMakeLists.txt has nothing fancy

But under windows (using VS 17 native compiler), there is a piece of code which doesnt even build and I don't get why. The error that I get is (sorry it is in french but pretty understandable I think):

error C2131: l'expression n'a pas été évaluée en constante    
note: échec en raison de l'appel d'une fonction indéfinie ou 'constexpr' non déclarée
note: voir l'utilisation de 'std::vector<ROI,std::allocator<_Ty>>::size'
error C3863: le type de tableau 'float ['fonction'+]['fonction'+]' n'est pas attribuable

And the (simplified) piece of code causing the error :

// Defined somewhere else
class ROI
{
}

class Tracker
{
public:
    void UpdateTrack(vector<ROI> new_roi)
    {
        // some code
        float match_table[new_roi.size() + 1][m_tracked_roi.size() + 1];  // COMPILE ERROR
        // some code
    }

private:
    vector<ROI> m_tracked_roi;
}

I think the problem is about the size of the array being known only at compile time or something like that, but it is possible with c++ now, and it works fine on Linux (by working I mean it builds and runs fine).

Can someone explain me what's goind on? and how to fix this on windows? (probably some additional CMake options?)

Thanks in advance

标签: c++ cmake
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-09 17:53

Variable length arrays are not part of standard C++. Array bounds must be compile-time constant expressions.

GCC and Clang both provide VLAs as an extension, but VisualStudio does not. Use std::vector if you need a cross-platform non-constant length array.

查看更多
登录 后发表回答