Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Error C2131 and C3863 on Windows but NOT on Linux [duplicate]

Tags:

c++

cmake

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

like image 841
Pierre Baret Avatar asked Dec 13 '25 17:12

Pierre Baret


1 Answers

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.

like image 168
Miles Budnek Avatar answered Dec 16 '25 08:12

Miles Budnek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!