【问题标题】:Can someone explain me, why my code is not working?有人可以解释一下,为什么我的代码不起作用?
【发布时间】:2018-05-26 12:36:26
【问题描述】:

我是 C++ 的新手,我必须解决大学的任务,我必须制作一个结构矩阵并用随机整数填充它。我用“!”标记了该行出现错误的地方。 这是错误 C2131(Visual C++ 编译器)。它说“表达式没有计算为常数”。

struct Matrix{
   int rows;
   int columns;
   Matrix(int r, int c){
      rows = r, columns = c;
   }
   int produceMatrix(){
       int matrix[rows][columns];  "!"
       for(int i = 0; i != rows; i++){
           for(int j = 0; j != columns; j++){
               matrix[i][j] = rand() %10 +1;
           }
       }
       return 0;
   }
   int showMatrix(){
       for(int i = 0; i != rows; i++){
           for(int j = 0; j != columns; j++){
               cout << matrix[i][j]<< endl;
           }
       }
   }
};


int main()
{
    srand(time(0));
    Matrix a(3, 4);

}    

【问题讨论】:

  • 因为 C++ 没有变长数组。 rowscolumns 需要是编译时常量才能工作
  • 我对 C++ 也不是很熟悉,但我想在结构中定义函数是不可能的。您需要改用“类”
  • @mangusta 完全错误。 structclass 在 C++ 中是一样的(除了默认访问和继承修饰符)
  • @UnholySheep 是的,我重新检查了 C++ 规范,你是对的
  • 另外,在一个类方法中声明的向量/数组与在另一个类方法中使用的任何东西完全无关。您试图在一个类方法中声明一个本地数组/向量,并在另一个类方法中使用它。 C++ 不能以这种方式工作。您的数组/向量必须是类成员。与其在每个问题上来回访问 stackoverflow.com,不如阅读你的 C++ 书籍。 stackoverflow.com 不是 C++ 教程,也不能替代 good C++ book

标签: c++


【解决方案1】:

如果您打算使用仅在运行时知道的rowscolumns 值创建矩阵,您最好使用std::vector&lt;std::vector&lt;int&gt;&gt; 来保存您的数据,因为您使用的静态数组需要知道它的大小编译时间。但是如果你所有的尺寸在编译时都是已知的,而你只是想灵活地创建不同的矩阵尺寸,你可以使用模板,例如:

#include <iostream>
#include <ctime>

template <int ROWS, int COLUMNS>
struct Matrix
{
    int rows = ROWS;
    int columns = COLUMNS;
    int matrix[ROWS][COLUMNS] = {};

    void produceMatrix()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                matrix[i][j] = rand() % 10 + 1;
            }
        }
    }

    void showMatrix()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                std::cout << matrix[i][j] << "\t";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    srand(time(0));
    Matrix<3,4> a;
    a.produceMatrix();
    a.showMatrix();
}

https://ideone.com/rCLxSn

4   10  5   5   
3   8   3   6   
2   4   9   10

【讨论】:

    【解决方案2】:

    有一件事是您不能以这种方式制作可变长度数组。 另一件事是,如果您在函数中创建变量(就像您在 produceMatrix() 中使用 int matrix 所做的那样),那么它在另一个函数中不可见。

    因此,应该在您的结构中声明包含矩阵元素的数组,在那里您声明了rowscolumns

    要存储矩阵的元素,您可以使用长度等于 rows*columns 的一维数组。

    现在,您需要某种动态数组才能使其长度在编译时未知。 一种解决方案是使用指针并在构造函数中使用 new 运算符定义数组。但是,如果你使用new,那么你必须在某些时候使用delete 来释放内存,这意味着需要析构函数。另一个问题是复制矩阵。 另一个更简单的解决方案是使用std::vector,这是一个由 c++ 标准库提供的容器。以下是使用std::vector 的方法(您需要将#include&lt;vector&gt; 添加到您的文件中):

    struct Matrix{
    int rows;
    int columns;
    vector<int> matrix;
    
    Matrix(int r, int c){
    rows = r, columns = c;
    matrix = vector<int>(c*r);
    }
    int produceMatrix(){
    
        for(int i = 0; i < matrix.size(); i++){
            matrix[i] = rand() %10 +1;
        }
        return 0;
    }
    int showMatrix(){
        for(int i = 1; i <= matrix.size(); i++){
            cout << matrix[i-1];
            if(i%columns == 0) cout << endl;
            else cout << " ";
        }
        return 0;
    }
    };
    

    【讨论】:

      【解决方案3】:

      正如许多人评论的那样,请阅读一本好的 C++ 书籍来了解数组、类、结构等。至于您的代码,以下可能会产生我认为您想要的:

      #include <iostream>
      #include <vector>
      
      struct Matrix
      {
          int rows;
          int columns;
          std::vector<std::vector<int>> matrix;
      
          Matrix(int r, int c): rows(r), columns(c)
          {
              matrix.resize(r);
              for(int i = 0; i < r; i++)
                  matrix[i].resize(c);
          }
          int produceMatrix()
          {
              for(int i = 0; i != rows; i++)
                  for(int j = 0; j != columns; j++)
                     matrix[i][j] = rand() %10 +1;
              return 0;
          }
      
          int showMatrix()
          {
              for(int i = 0; i != rows; i++)
              {
                  for(int j = 0; j != columns; j++)
                      std::cout << matrix[i][j]<<" ";
              }
              std::cout<<'\n';
          }
      };
      
      int main()
      {
          srand(time(0));
          Matrix a(3, 4);
          a.produceMatrix();
          a.showMatrix();
      }
      

      【讨论】:

      • “resize”的目的是什么?
      • @Josh.K: resize 将向量的大小设置为我们想要的值,同时分配内存。所以稍后,我们可以简单地用 operator[] 来引用元素。
      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 2023-04-02
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多