【发布时间】:2014-04-23 10:35:21
【问题描述】:
我想为以下代码声明一个函数,该函数是从文本文件中读取的矩阵。代码如下。
if (infile == "A.txt")
{
ifstream myfile("A.txt");
string line;
int MatA[3][3];
int i=0;
while (getline (myfile, line))
{
stringstream ss(line);
for(int j=0; j<3; j++)
ss >> MatA[i][j]; // load the i-th line in the j-th row of mat
i++;
}
// display the loaded matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<MatA[i][j]<<" ";
cout<<endl;
}
}
现在我尝试将这个矩阵声明为一个函数,以便稍后在我的代码中执行操作时,我可以只调用该函数,而不必重新编写整个矩阵。但是我很难做到这一点,我尝试将矩阵声明为函数如下所示。
int display (int MatA)
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<MatA[i][j]<<" ";
cout<<endl;
}
}
但是出现了一个错误,说[i]'表达式必须有一个指向对象类型的指针'。
如果有人能帮忙那就太好了!
【问题讨论】:
-
您传递的是
int,而对于二维数组,您应该传递int **
标签: c++ arrays function variables function-declaration