简体   繁体   English

c ++对函数执行操作并声明函数

[英]c++ performing operations on functions and declaring functions

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! 如果有人可以帮助,那就太好了!

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. 最后,您可以使用@boycy发表的评论中建议的方法,但对我而言,我不喜欢这种方法。 For example 例如

#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 );
}

You are passing an int MatA into display, but you want int[][] as an argument. 您正在将int MatA传递给显示,但是您希望int[][]作为参数。 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**并对其执行指针算术,要么必须为此矩阵创建一些更好的访问器。

I would suggest taking a look of similar implementations like the OpenCV Mat type that addresses your issues. 我建议您看一下类似的实现,例如可解决您问题的OpenCV Mat类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM