c++ - int c++ 之前预期的不合格 id

标签 c++ arrays function reference declaration

有人能找出我的错误吗?

#include<iostream>

using namespace std;

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

当我构建时显示“'int'之前的预期不合格 ID”

最佳答案

看来你的意思是下面的

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

关于c++ - int c++ 之前预期的不合格 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41435386/

相关文章:

c++ - 从函数返回 unordered_map 内的 unordered_map

android - 对于显示 UnsatisfiedLinkError : No implementation found for . stringFromJNI2() 的第二个 native 函数

c - 我的 returnList[0] 被重写为 @5'

php - Codeigniter 重建查询输出数组以插入另一个表中

python - 如何有效地创建一个多维 numpy 数组,其条目仅取决于一维索引?

Javascript:暂停/取消暂停 javascript 俄罗斯方 block 游戏

ios - swift 函数和闭包可以符合 Hashable 吗?

C - 没有使用参数指针获得正确的值

c++ - 无法在用 C++ 编写的 UWP 上使用 SQLite 打开数据库

C++ 名称解析