简体   繁体   English

有人可以解释我,为什么我的代码不起作用吗?

[英]Can someone explain me, why my code is not working?

I am completely new in C++ and I have to solve a task for college, where I have to make a struct Matrix and fill it with random integers. 我是C ++的新手,我必须解决一个大学的任务,在那里我必须制作一个struct Matrix并将其填充为随机整数。 I marked the line with a "!" 我在行上标了“!” where the error appears. 错误出现的位置。 It is the error C2131(Visual C++ Compiler). 这是错误C2131(Visual C ++编译器)。 It says "expression did not evaluate to a constant". 它说:“表达式没有求出常数”。

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

}    

If you are planning to create your matrix with rows and columns values only known at runtime, you are better off using std::vector<std::vector<int>> to hold your data, as the static array you use needs to know its size at compile time. 如果打算使用仅在运行时知道的rowscolumns值来创建矩阵,则最好使用std::vector<std::vector<int>>来保存数据,因为使用的静态数组需要知道在编译时的大小。 But if all your sizes are known at compile time and you just want flexibility of creating different matrix sizes, you can use template, for example: 但是,如果在编译时知道所有大小,并且只想灵活创建不同的矩阵大小,则可以使用模板,例如:

#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 https://ideone.com/rCLxSn

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

One thing is that you cannot make variable-length arrays this way. 一件事是您不能以这种方式制作可变长度数组。 Another thing is that if you create a variable within a function (like you were doing here with int matrix in produceMatrix() ), it is then not visible in another function. 另一件事是,如果您在一个函数中创建一个变量(就像您在此处使用produceMatrix() int matrix produceMatrix() ),则该变量在另一个函数中不可见。

Therefore, the array containing the elements of the matrix should be declared in your struct there, where you have declared rows and columns . 因此,包含矩阵元素的数组应在已声明rowscolumns结构中声明。

To store the elements of your matrix, you can use one-dimensional array of length equal to rows*columns . 要存储矩阵的元素,可以使用长度等于rows*columns一维数组。

Now, you need some kind of dynamic array to be able to make it of the length not known in the compilation time. 现在,您需要某种动态数组才能使其具有编译时未知的长度。 One solution is to use a pointer and define an array with new operator in the constructor. 一种解决方案是使用指针,并在构造函数中使用new运算符定义一个数组。 However, if you use new , then you have to use delete at some point to deallocate memory, which here means that the destructor is needed. 但是,如果使用new ,则必须在某些时候使用delete来释放内存,这意味着需要析构函数。 Another problem would be with copying of your matrices. 另一个问题是矩阵的复制。 Another, simpler solution is to use std::vector , a container provided by c++ standard library. 另一个更简单的解决方案是使用std::vector ,这是c ++标准库提供的容器。 Here's how to do it with std::vector (you need to add #include<vector> to your file): 这是使用std::vector (您需要在文件中添加#include<vector> ):

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

As many people commented, please go through a good C++ book to learn about arrays, classes, structs etc. As for your code, the following might produce what I think you want: 正如许多人评论的那样,请通读一本不错的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();
}

暂无
暂无

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

相关问题 有人可以向我解释为什么这段代码没有 output 任何东西 - Can someone explain to me why this code doesn't output anything 谁能解释我为什么此代码不起作用? - Can anybody explain me why this code is not working? 有人可以向我解释这段代码的作用吗? - Can someone explain to me what this code does? 奇怪的代码...有人可以向我解释一下 - Strange code… Can someone explain me this 有人可以向我解释为什么我的字符串没有在 output 中显示它经常发生在我身上,这是一个简单的例子, - can someone explain to me why my string is not not showing in output it happens to me a lot and this is a simple example, 有人可以帮我弄清楚为什么我的代码不能正常工作吗? - Can someone help me figure out why my code isn't working properly? 有人可以向我解释为什么这是我的输出吗? 以及我将如何纠正我的输出? - Can someone explain to me why my output is this? And how would I correct my output? 有人可以向我解释为什么在LLVM的以下代码中使用相同的操作数进行不等式测试? - Can someone explain to me why there is an inequality test with the same operands in the following code from LLVM? 有人可以向我解释这个 makefile 吗? - Can someone explain this makefile to me? 有人可以解释我这段代码吗? 它给出了我们输入的数字的反面 - Can someone explain me this code? It gives the reverse of the number we enter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM