【问题标题】:Argument of type int(*)[] is incompatible with parameter of type "int**"int(*)[] 类型的参数与“int**”类型的参数不兼容
【发布时间】:2021-03-08 14:55:42
【问题描述】:

我正在创建一个函数,它会给我一个矩阵的随机值。我知道如何在main 函数中实现它,但我想要一个单独的函数。我也尝试使用void 并且发生了同样的事情,并且我不断收到同样的错误。我是初学者,我知道这个问题与指针有关,但我仍然不知道我应该怎么做才能让它工作并修复它。我已经用 Google 搜索过了,但我无法完成这项工作。

#include<stdlib.h>
#include<iostream> 
#include<time.h>

int row, coll;

int random(int *mat[])
{
    srand(time(0));
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<coll; j++)
        {
            mat[i][j] = rand()%10;
        }
    }

}

main()
{
    std::cin >> row >> coll;
    int mat[row][coll];
    random(mat);
} 

【问题讨论】:

  • std::cin &gt;&gt; row &gt;&gt; coll; int mat[row][coll]; -- 这不是有效的 C++。 C++ 中的数组的维度必须由编译时表达式指定,而不是运行时变量。
  • 数组不是指针。特别是,数组数组不是指针数组。
  • 它也不是有效的 C,因为尽管 C 有可变长度数组(作为可选功能),但它没有 iostream。
  • 无法完成。 random 在编译时必须知道除了最外层之外的所有维度。这对于可变长度数组是不可能的。他们are not included in Standard C++ 的众多原因之一。我可以推荐a simple matrix class backed by a std::vector吗?
  • @klutt 你不必说服我。我是std::vectorfanboy,我从不写任何 C ;)

标签: c++ multidimensional-array parameter-passing implicit-conversion function-definition


【解决方案1】:

您正试图在一个程序中混合使用两种不同语言的功能。

显示的程序不是有效的 C++ 程序,也不是有效的 C 程序。

例如你的程序中的可变长度数组

std::cin >> row >> coll;
int mat[row][coll];

不是标准的 C++ 功能。

函数 main 的返回类型应为int

你的函数random

int random(int *mat[])

返回类型为int,但不返回任何内容。

参数具有类型(如果假设支持可变长度数组)

int ( * )[coll]

但函数参数类型是

int **.

如果您要编写 C++ 程序,则使用标准容器 std::vector&lt;std::vector&lt;int&gt;&gt; 代替可变长度数组。

例如

std::cin >> row >> coll;

std::vector<std::vector<int>> mat( row, std::vector<int>( coll ) );

要不然写个C程序,看起来像

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void random( size_t row, size_t col, int mat[row][col], int limit )
{
    srand( ( unsigned int )time( NULL ) );
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            mat[i][j] = rand() % limit;
        }
    }
}

int main( void ) 
{
    size_t row, col;
    
    scanf( "%zu %zu", &row, &col );
    
    int mat[row][col];
    
    random( row, col, mat, 10 );
    
    return 0;
}

【讨论】:

  • 好的,所以我尝试按照您告诉我的操作,我尝试了 C 方法,因为我仍然不知道如何使用向量,现在我收到另一条消息,错误在 for ( size_t j = 0; j
  • @DimitrijeDjokic 我展示了一个可以编译和工作的演示程序。复制并粘贴它。
  • 我收到一个错误,指出该参数是不允许的,在其 void random(int mat[row][col]) 行和 col 带下划线的函数中。
  • 我学习了 3 个小时,我尝试了很多不同的东西,看了很多教程,但我不断收到消息“无法转换 'int(*)[col]....等等。 ”。即使在您的代码中,这是我得到的唯一错误消息,我设法弄清楚了其他所有内容。我只是不知道为什么这个特定的事情会发生在我身上,我尝试一步一步地做某人做过的事情,但我一遍又一遍地遇到同样的错误,就像我说的那样,即使使用你的代码,我也知道为什么我会遇到其他错误,我修复了那些。我在这里重复自己,但没关系。再次感谢您的帮助。
  • @DimitrijeDjokic C 编译器应该支持 C99 标准。
【解决方案2】:

我为你写了一个简单的矩阵结构来携带函数周围的数据。此外,输出过载使其更易于演示。请查看它,如果您需要更多详细信息,请不要犹豫。

#include<stdlib.h>
#include<iostream>
#include<time.h>
#include <iomanip>
struct Matrix
{
    int *arr;
    const int row, col;
    Matrix() = delete;
    Matrix(const int i, const int j): row(i), col(j)
     {
       arr = new int [i*j];
     }
   ~Matrix() { delete [] arr; }
   int operator()(const int i, const int j) const {return arr[i*col+j];}
   int&operator()(const int i, const int j) {return arr[i*col+j];}
};
std::ostream& operator<<(std::ostream&osm, const Matrix&M)
{
    for (int i=0; i<M.row; i++) {
        for (int j=0; j< M.col; j++)  osm << std::setw(8) << M(i,j);
        osm << std::endl;
    }
    return osm;
}
void random(Matrix& amtx)
{
   srand(time(0));
   for(int i=0; i<amtx.row; i++)
    {
       for(int j=0; j<amtx.col; j++)
        {
           amtx(i,j) = rand()%10;
        }
    }
}
int main()
{
    int r, c;
    std::cout << "Input row and column : ";
    std::cin >> r >> c;
    Matrix mtx(r, c);
    random(mtx);
    std::cout << mtx;
   return 0;
}

和测试运行:

Input row and column :  5  5
      6       3       1       9       3
      6       5       1       4       1
      1       2       7       5       5
      9       3       2       8       1
      7       4       8       3       5

【讨论】:

  • 朝着正确的方向前进,但 Matrix 泄漏内存。当您通过添加析构函数来修复泄漏时,您必须观察the Rule of Three or the Rule of Five
  • 这对我来说太复杂了 :D 我是初学者,我不知道这个方法是如何工作的。谢谢你的帮助 :D 祝你有美好的一天。
  • @user4581301 我忘了添加 destrcutor。已编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多