c++ - 我无法让我的简单程序将二维数组传递给函数

标签 c++ arrays reference 2d

我尝试了几种方法,但我无法让函数“Matrixinputs”通过引用接受二维数组。 “Matrixinputs”将把数组的输入更改为用户选择的内容。我是初学者,但我认为这与我试图传递其参数由用户定义的动态数组这一事实有关,但这只是一个猜测。请帮忙,我的错误是这样的

matrix2.C:15:11: error: invalid operands of types ‘int [(((sizetype)(((ssizetype)a) + -1)) + 1)][(((sizetype)(((ssizetype)b) + -1)) + 1)]’ and ‘int [(((sizetype)(((ssizetype)c) + -1)) + 1)][(((sizetype)(((ssizetype)d) + -1)) + 1)]’ to binary ‘operator*’
  cour<<m1*m2;

$ g++ matrix.C -omatrixs -lm
matrix.C: In function ‘int main()’:
matrix.C:16:25: error: expression list treated as compound expression in initializer [-fpermissive]

这是我的代码

#include <iostream>
#include <cmath>
using namespace std;
//Prototypes
double Matrixsettings(int&, int&, int&, int&);
int Matrixinputs(int&, int &);

int main()
{
    int a=2, b=2, c=2, d=2;
    cout<<  Matrixsettings( a,b,c,d);
    cout<< a<<b<<c<<d;
    int m1 [a] [b], m2 [c] [d];
    cout<<m1 [a] [b]<< m2 [c] [d];
    int Matrixinputs(m1 [a] [b],m2 [c] [d]);
return 0;
}

double Matrixsettings( int &a, int &b,  int &c, int &d)
{ 
    cout<< "how many rows in the first matrix: ";
    cin>> a;
    cout<< "how many columns in the first matrix: ";
    cin>> b;
    cout<< "how many rows in the second matrix: ";
    cin>> c;
    cout<< "how many columns in the second matrix: ";
    cin>> d;
    return 0;
}

int Matrixinputs(m1& [a] [b],m2& [c] [d]);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array
}

最佳答案

void Matrixinputs(int* matrix, int row, int column);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array

for (int i=0; i<row; i++)
{
    for (int j=0; j<column; j++)
    {
        cin >> matrix[i*row+column];
    }
}
}

在此函数之外,手动为矩阵指针分配内存。

关于c++ - 我无法让我的简单程序将二维数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580380/

相关文章:

perl - 如何将模块的函数作为对 Perl 中另一个模块的引用传递?

c++ - 是否可以在没有任何外部库的情况下在 c++/linux 的窗口中显示 bmp 图像?

PHP对象嵌套问题

C++ - 何时为使用但未分配的对象调用析构函数?

javascript - 无法将对象推送到 localStorage 中的数组中

c - 如何为 C 中的二维数组位置赋值?

比较内存地址

c# - 如何从解决方案中的类访问我的 ASP.NET 页面上的控件?

c++ - 在 localtime() 中崩溃,但 gmtime() 有效

C++头文件——语法问题