在Windows上编译错误C2131和C3863,在Linux上编译错误 [英] Compile Error C2131 and C3863 on Windows but NOT on Linux
问题描述
我有一段代码可以在Linux(Raspbian)上编译并正常工作,但不能在Windows(VS 17)上编译。
I have a piece of code that compiles and works fine on Linux (Raspbian) but doesn't compile on windows (VS 17).
我正在使用CMAKE 3用于跨平台编译,就像我说的那样,我在Linux上构建它没有问题。
I am using CMAKE 3 for cross platform compiling, and like I said, I have no problem building this on Linux.
这里是我正在使用的唯一CMAKE选项:
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
但是在Windows下(使用VS 17本机编译器),有一段代码甚至没有构建,我不明白为什么。我得到的错误是(抱歉,它是法文,但我认为这是可以理解的):
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;
}
我认为问题在于仅在编译时才知道数组的大小时间或类似的东西,但是现在c ++可以使用,并且它在Linux上也可以正常工作(通过工作,我是说它可以正常构建和运行)。
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).
有人可以解释吗我怎么了?以及如何在Windows上解决此问题? (可能还有其他一些CMake选项?)
Can someone explain me what's goind on? and how to fix this on windows? (probably some additional CMake options?)
预先感谢
推荐答案
可变长度数组不是标准C ++的一部分。数组边界必须是编译时常量表达式。
Variable length arrays are not part of standard C++. Array bounds must be compile-time constant expressions.
GCC和Clang都提供VLA作为扩展,但VisualStudio不提供。如果需要跨平台非恒定长度数组,请使用 std :: vector
。
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.
这篇关于在Windows上编译错误C2131和C3863,在Linux上编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!