2014-10-07 2 views
0

나는 이걸 베어 본 코드에 스트라이프했습니다. 기본적으로 함수에 2 차원 배열을 전달해야하지만 배열의 크기는 실행시 텍스트 파일에서 읽습니다. 이 주제에서 읽은 모든 것은 이것이 이것을 수행하는 방법이라고 말하지만 컴파일러는 그렇지 않다고 말합니다. 코드는 다음과 같습니다.왜 Code :: Blocks에서 mingw와 함께 컴파일되지 않습니까?

#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; 
} 

나는 매우 익숙하지 않기 때문에 벡터를 피합니다. 이 코드 Array는 통상 C++ 다차원 배열이 아닌

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

에서

-------------- 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)) 

답변

2

하지만 C99 가변 길이 배열 또는 VLA이 컴파일러의 출력이다.

표준 C++가 아닙니다.

대신 이니셜 표현이 컴파일 타임에 알려져 있기 때문에이 작동

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

않습니다.


은 g ++ 호출에 옵션 -pedantic-errors를 추가, 이러한 문제를 방지하려면.

+0

방금 ​​"행"과 "열"을 대문자로 사용했습니다. 행과 열의 개수는 런타임에 텍스트 파일에서 읽혀집니다. 주어진 일을 계속할 수 있습니까? – bobsicle0

+0

@ bobsicle0 nope. 텍스트 파일에서 읽은 숫자는 컴파일 할 때 알 수 없습니다. 저장을 위해'std :: vector'를 사용하십시오. –

관련 문제