【问题标题】:C++ - Passing array from function to functionC++ - 将数组从函数传递到函数
【发布时间】:2017-01-07 23:02:26
【问题描述】:

所以我有这个代码:

main.cpp

#include "matrix.h"

int main(int argc, char *argv[])
{


    matrix m;
    regularMatrix rMatrix;

    rMatrix.setDetails(3, "1 2 3 4 5 6 7 8 9");
    //rMatrix.displayMatrix();

    //int i, j, r = 0, c = 0, a;


    //cout << "Enter number of Rows and Columns: " << endl;

    //cin >> r ;












    system("PAUSE");
    return EXIT_SUCCESS;
}

矩阵.cpp

#include "matrix.h"

int rows = 3, columns = 3;
int **a;




void matrix::displayMatrix(int **arr)
{

    cout  <<  "Values Of 2D Array [Matrix] Are : ";
    for  (int i  =  0;  i  <  rows;  i++  )
    {
         cout  <<  " \n ";
         for  (int j  =  0;  j  <  columns;  j++  )
         {
              cout <<  arr[i][j] << "    ";
         }
    }
}

void matrix::setDetails(int dimension, string y)
{
     int f = dimension;
     rows = dimension;
     columns = rows;
     string input = y;

     istringstream is(input);
     int n;

     a = new int *[rows];
     for(int i = 0; i <rows; i++)
     {
             a[i] = new int[columns];
     }   



     for  ( int i  =  0;  i  <  rows;  i++  )
     {
          for  ( int j  =  0;  j  <  columns;  j++  )
          {
               while ( is >> n)
               {
                     a[i][j] = n;
                     //cout << a[i][j] << endl;
               }
          }
     }



     matrix::displayMatrix(a);

     //cout << f << endl << g << endl;
}

矩阵.h

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class matrix
{
      public:
             virtual void displayMatrix(int** arr);
             virtual void setDetails(int dimension, string y);
             //virtual void setMatrix(int m[]);
             //virtual void printArray(int** i);


};

class regularMatrix : public virtual matrix
{
      public:


};

它运行没有错误,但问题是,我在显示矩阵时得到不同的值?我想我正在获取数组的地址。如何从中获取价值?我认为传递我的数组是正确的。

【问题讨论】:

  • 考虑在手表窗口打开的情况下使用调试器工具并逐步完成代码。处理这类问题真的很不错
  • 考虑使用 std::vector 代替数组。所有这些问题(包括你没有看到的内存泄漏)都会消失 :)。
  • 您能否提供一个minimal reproducible example 来重现您的问题。还要添加有关您在调试代码期间观察到的内容的信息。您可能会从 arr[i][j] 输出未初始化的值,而不是 addresses
  • @Zelgh 奇怪的要求,实际上是什么组成的?
  • 你总是可以在需要时从一个向量中得到一个 c 样式的数组,因此即使你必须传递数组,我也会在我的代码中使用一个向量(或 std::array 在适当的时候)

标签: c++ arrays function multidimensional-array parameter-passing


【解决方案1】:
 for  (  i  =  0;  i  <  rows;  i++  )
 {
      for  (  j  =  0;  j  <  columns;  j++  )
      {
           while ( is >> n)
           {
                 a[i][j] = n;
                 //cout << a[i][j] << endl;
           }
      }
 }

这实际上是非常错误的。看看你在这里做什么。 一开始,你有i = 0 and j = 0

然后你进入了while 循环。

在这里,直到您从 stringstream 输入整数,您将 a[0][0] 分配给新值。 看见?你永远不会去 a[0][1] 等等。只有第一个元素是有效的,其余的将保持未初始化,因为在你的 while 循环第一次执行之后 istringstream 对象中没有剩余字符。

所以纠正它:

for  (  i  =  0;  i  <  rows;  i++  )
     {
          for  (  j  =  0;  j  <  columns;  j++  )
          {
              if ( is >> n )
              a[i][j] = n;
          }
     }

【讨论】:

  • 没问题,我添加了一个代码,如果您遇到一些问题应该会有所帮助,但是我很确定您已经自己解决了。
  • 感谢指正,我没想到!
猜你喜欢
  • 2013-04-26
  • 2020-07-17
  • 2018-07-28
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多