int c ++之前的预期unqualified-id

时间:2017-01-03 01:52:32

标签: c++ arrays function reference declaration

有人能找到我的错误吗?

#include<iostream>

using namespace std;

void (int n, int &M[][]){
//here comes my code
}

当我构建节目&#34;期望不合格的id之前&#39; int&#39; &#34;

1 个答案:

答案 0 :(得分:1)

看来你的意思是以下

template <size_t N>
void process_matrix( int ( &M )[N][N] )
{
    //here comes my code
}

这是一个示范程序

#include <iostream>

template <size_t N>
void process_matrix(int(&m)[N][N])
{
    for (size_t i = 0; i < N; i++)
    {
        for (size_t j = 0; j < N; j++) m[i][j] = i * N + j;
    }

    for (const auto &row : m)
    {
        for (int x : row) std::cout << x << ' ';
        std::cout << std::endl;
    }
}

int main()
{
    int m1[2][2];

    process_matrix(m1);

    std::cout << std::endl;

    int m2[3][3];

    process_matrix(m2);

    std::cout << std::endl;

    return 0;
}

它的输出是

0 1
2 3

0 1 2
3 4 5
6 7 8