簡體   English   中英

有人可以解釋我,為什么我的代碼不起作用嗎?

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

我是C ++的新手,我必須解決一個大學的任務,在那里我必須制作一個struct Matrix並將其填充為隨機整數。 我在行上標了“!” 錯誤出現的位置。 這是錯誤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);

}    

如果打算使用僅在運行時知道的rowscolumns值來創建矩陣,則最好使用std::vector<std::vector<int>>來保存數據,因為使用的靜態數組需要知道在編譯時的大小。 但是,如果在編譯時知道所有大小,並且只想靈活創建不同的矩陣大小,則可以使用模板,例如:

#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

一件事是您不能以這種方式制作可變長度數組。 另一件事是,如果您在一個函數中創建一個變量(就像您在此處使用produceMatrix() int matrix produceMatrix() ),則該變量在另一個函數中不可見。

因此,包含矩陣元素的數組應在已聲明rowscolumns結構中聲明。

要存儲矩陣的元素,可以使用長度等於rows*columns一維數組。

現在,您需要某種動態數組才能使其具有編譯時未知的長度。 一種解決方案是使用指針,並在構造函數中使用new運算符定義一個數組。 但是,如果使用new ,則必須在某些時候使用delete來釋放內存,這意味着需要析構函數。 另一個問題是矩陣的復制。 另一個更簡單的解決方案是使用std::vector ,這是c ++標准庫提供的容器。 這是使用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;
}
};

正如許多人評論的那樣,請通讀一本不錯的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM