【发布时间】:2014-10-07 03:51:53
【问题描述】:
我已将其简化为基本代码。本质上,我需要将二维数组传递给函数,但数组的大小是在执行时从文本文件中读取的。我读过的关于这个主题的所有内容都说这是这样做的方法,但编译器却说不然。代码如下:
#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))
【问题讨论】:
标签: c++ arrays compiler-errors g++ mingw