繁体   English   中英

为什么在Code :: Blocks中不能用mingw编译?

[英]Why will this not compile with mingw in Code::Blocks?

我已经将其简化为基本代码。 本质上,我需要将2d数组传递给函数,但是执行时会从文本文件中读取数组的大小。 我在该主题上阅读的所有内容都表明这是做到这一点的方法,但编译器则相反。 这是代码:

#include <iostream>

using namespace std;

template <size_t r, size_t c>
void func(int (&a)[r][c])
{
    return;
}

int main()
{
    int rows = 5;
    int cols = 6;
    int Array[rows][cols];

    func(Array);

    return 0;
}

我宁愿避免使用向量,因为我对它们非常不熟悉。 这是编译器的输出:

-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g  -c C:\Users\ME\Desktop\test\test\main.cpp -o obj\Debug\main.o
C:\Users\ME\Desktop\test\test\main.cpp: In function 'int main()':
C:\Users\ME\Desktop\test\test\main.cpp:20:15: error: no matching function for call to 'func(int [(((unsigned int)(((int)rows) + -0x000000001)) + 1)][(((unsigned int)(((int)cols) + -0x000000001)) + 1)])'
C:\Users\ME\Desktop\test\test\main.cpp:20:15: note: candidate is:
C:\Users\ME\Desktop\test\test\main.cpp:6:25: note: template<unsigned int r, unsigned int c> void func(int (&)[r][c])
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

在这段代码中

int rows = 5;
int cols = 6;
int Array[rows][cols];

Array不是普通的C ++多维数组,而是C99 可变长度数组或VLA。

它不是标准的C ++。

相反做

int const rows = 5;
int const cols = 6;
int Array[rows][cols];

这是可行的,因为在编译时就知道了初始化程序表达式。


为避免此类问题,请在您的g ++调用中添加选项-pedantic-errors

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM