[英]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.