在 Windows 上编译错误 C2131 和 C3863,但在 Linux 上则不然

Pie*_*ret 3 c++ cmake

我有一段代码可以在 Linux (Raspbian) 上编译并正常工作,但不能在 Windows (VS 17) 上编译。

\n\n

我使用 CMAKE 3 进行跨平台编译,就像我说的,我在 Linux 上构建它没有问题。

\n\n

以下是我使用的唯一 CMAKE 选项:

\n\n
cmake_minimum_required(VERSION 3.1)\nproject(Track)\nset (CMAKE_CXX_STANDARD 11)\n...\n// The rest of the CMakeLists.txt has nothing fancy\n
Run Code Online (Sandbox Code Playgroud)\n\n

但在Windows下(使用VS 17本机编译器),有一段代码甚至无法构建,我不明白为什么。我得到的错误是(抱歉,它是法语,但我认为很容易理解):

\n\n
error C2131: l'expression n'a pas \xc3\xa9t\xc3\xa9 \xc3\xa9valu\xc3\xa9e en constante    \nnote: \xc3\xa9chec en raison de l'appel d'une fonction ind\xc3\xa9finie ou 'constexpr' non d\xc3\xa9clar\xc3\xa9e\nnote: voir l'utilisation de 'std::vector<ROI,std::allocator<_Ty>>::size'\nerror C3863: le type de tableau 'float ['fonction'+]['fonction'+]' n'est pas attribuable\n
Run Code Online (Sandbox Code Playgroud)\n\n

以及导致错误的(简化的)代码:

\n\n
// Defined somewhere else\nclass ROI\n{\n}\n\nclass Tracker\n{\npublic:\n    void UpdateTrack(vector<ROI> new_roi)\n    {\n        // some code\n        float match_table[new_roi.size() + 1][m_tracked_roi.size() + 1];  // COMPILE ERROR\n        // some code\n    }\n\nprivate:\n    vector<ROI> m_tracked_roi;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我认为问题在于数组的大小仅在编译时或类似的情况下才知道,但现在使用 c++ 是可能的,并且它在 Linux 上运行良好(我所说的工作是指它构建和运行良好)。

\n\n

有人可以向我解释一下发生了什么事吗?以及如何在 Windows 上解决这个问题?(可能还有一些额外的 CMake 选项?)

\n\n

提前致谢

\n

Mil*_*nek 5

可变长度数组不是标准 C++ 的一部分。数组边界必须是编译时常量表达式。

GCC 和 Clang 都提供 VLA 作为扩展,但 VisualStudio 不提供。std::vector如果您需要跨平台的非恒定长度数组,请使用。

  • C 和 C++ 是不同的语言。VLA 是 C 的一部分,但不是 C++ 的一部分。 (4认同)