【问题标题】:expected unqualified-id before int c++在 int c++ 之前预期 unqualified-id
【发布时间】:2017-01-03 01:52:31
【问题描述】:

有人能找出我的错误吗?

#include<iostream>

using namespace std;

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

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

【问题讨论】:

  • 这个声明有太多问题了。您要声明什么(使用文字)?
  • 给你的函数一个名字,让你的第二个参数成为一个合理的类型
  • 我想在函数中放一个矩阵
  • 没有什么不是函数的有效名称。
  • 我把函数的名字写错了

标签: c++ arrays function reference declaration


【解决方案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

【讨论】:

  • 如何修复 N 值?
猜你喜欢
  • 2014-03-07
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2014-12-19
  • 1970-01-01
相关资源
最近更新 更多