I want to declare a function for the following code which is a matrix that is read from a text file. The code can be seen below.
我想为以下代码声明一个函数,这是一个从文本文件中读取的矩阵。代码如下所示。
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;
}
}
Now what I've tried to do is declare this matrix as a function so when I perform operations later on in my code I can just call the function rather than having to re-write the whole matrix. But I'm having difficulty doing this, the attempt I've made to declare the matrix as a function can be seen below.
现在我试图将这个矩阵声明为一个函数,所以当我稍后在我的代码中执行操作时,我可以调用函数而不必重写整个矩阵。但是我很难做到这一点,我已经尝试将矩阵声明为函数可以在下面看到。
int display (int MatA)
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<MatA[i][j]<<" ";
cout<<endl;
}
}
However an error occurs saying that [i]
'expression must have a pointer to object type'.
但是,如果[i]'表达式必须具有指向对象类型的指针',则会发生错误。
If anybody could help that'd be great!
如果有人能帮助那就太好了!
2 个解决方案
#1
1
For example the function can be defined the following way
例如,可以通过以下方式定义函数
const size_t N = 3;
void display( const int ( &MatA )[N][N] )
{
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << MatA[i][j] << " ";
std::cout << std::endl;
}
}
The other way is the following
另一种方式如下
const size_t N = 3;
void display( const int ( *MatA )[N], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << MatA[i][j] << " ";
std::cout << std::endl;
}
}
The functions can be called as
这些功能可以称为
#include <iostream>
const size_t N = 3;
// the function definitions
int main()
{
int a[N][N] = {};
// some code to fill the matrix
display( a );
display( a, N );
}
And at last you can use the approach suggested in comments to the post by @boycy though as for me then I do not like this approach. For example
最后你可以使用@boy在评论中建议的方法,但对于我来说,我不喜欢这种方法。例如
#include <iostream>
const size_t N = 3;
void display( const int **MatA, size_t m, size_t n )
{
for ( size_t i = 0; i < m * n; i++ )
{
std::cout << MatA[i] << " ";
if ( ( i + 1 ) % n == 0 ) std::cout << std::endl;
}
}
int main()
{
int a[N][N] = {};
// some code to fill the matrix
display( reinterpret_cast<const int **>( a ), N, N );
}
#2
0
You are passing an int MatA
into display, but you want int[][]
as an argument. This however does not work that way. So you either have to pass an int**
and perform pointer arithmetics on it or you will have to create some better accessor for this matrix.
您正在将一个int MatA传递给display,但是您想要int [] []作为参数。然而,这不起作用。所以你要么必须传递一个int **并在其上执行指针算术,否则你将不得不为这个矩阵创建一些更好的访问器。
I would suggest taking a look of similar implementations like the OpenCV Mat type that addresses your issues.
我建议看一下像OpenCV Mat类似的类似实现来解决你的问题。
#1
1
For example the function can be defined the following way
例如,可以通过以下方式定义函数
const size_t N = 3;
void display( const int ( &MatA )[N][N] )
{
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << MatA[i][j] << " ";
std::cout << std::endl;
}
}
The other way is the following
另一种方式如下
const size_t N = 3;
void display( const int ( *MatA )[N], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << MatA[i][j] << " ";
std::cout << std::endl;
}
}
The functions can be called as
这些功能可以称为
#include <iostream>
const size_t N = 3;
// the function definitions
int main()
{
int a[N][N] = {};
// some code to fill the matrix
display( a );
display( a, N );
}
And at last you can use the approach suggested in comments to the post by @boycy though as for me then I do not like this approach. For example
最后你可以使用@boy在评论中建议的方法,但对于我来说,我不喜欢这种方法。例如
#include <iostream>
const size_t N = 3;
void display( const int **MatA, size_t m, size_t n )
{
for ( size_t i = 0; i < m * n; i++ )
{
std::cout << MatA[i] << " ";
if ( ( i + 1 ) % n == 0 ) std::cout << std::endl;
}
}
int main()
{
int a[N][N] = {};
// some code to fill the matrix
display( reinterpret_cast<const int **>( a ), N, N );
}
#2
0
You are passing an int MatA
into display, but you want int[][]
as an argument. This however does not work that way. So you either have to pass an int**
and perform pointer arithmetics on it or you will have to create some better accessor for this matrix.
您正在将一个int MatA传递给display,但是您想要int [] []作为参数。然而,这不起作用。所以你要么必须传递一个int **并在其上执行指针算术,否则你将不得不为这个矩阵创建一些更好的访问器。
I would suggest taking a look of similar implementations like the OpenCV Mat type that addresses your issues.
我建议看一下像OpenCV Mat类似的类似实现来解决你的问题。