简体   繁体   English

在 c++ 中的 class 中声明一个矩阵

[英]declaring a matrix in class in c++

for the following code:对于以下代码:

    class graph
    {
    const int N,M;
    bool matrix[N][M];
    public:
    graph(int n,int m):N{n},M{m}
    {

    }
    };
    int main()
    {
       graph g=graph(5,6);
       return 0;
    }

This code gives error:此代码给出错误:

    error: invalid use of non-static data member 'graph::N'
    error: invalid use of non-static data member 'graph::M'

the data members are being initialized immediately while the object is created,right?创建 object 时,数据成员会立即初始化,对吗? what is the meaning of this error?这个错误是什么意思?

bool matrix[N][M]; is a fixed size 2d array, it's size must be known in compile time.是一个固定大小的二维数组,它的大小必须在编译时知道。 But in your code it is not.但在您的代码中并非如此。

You cannot declare variable size array.您不能声明可变大小的数组。 bool matrix[N][M] size should be determined at compile time. bool matrix[N][M]大小应在编译时确定。 Hence, you can make dynamic array with pointers as shown:因此,您可以使用指针创建动态数组,如下所示:

class Graph {
    const int N, M;
    bool **matrix;  // Pointer to a pointer

public:
    // Initializer list in the c'tor
    Graph(int x, int y) : M(x), N(y) {
        matrix = new bool*[N];

        for (int i{}; i < N; i++)
            matrix[i] = new bool[M];
    }
    // The d'tor
    ~Graph() {
        for (int i = 0; i < N; i++)
            delete[] matrix[i];
        delete[] matrix;
    }
};

You have two options.你有两个选择。

  1. Decided that N and M are compile-time constants and make your class a template.决定 N 和 M 是编译时常量,并使您的 class 成为模板。

Example:例子:

 template<int N, int M>
 class graph
 {
     bool matrix[N][M];
 };

Here graph<3,3> would be a type-id of class type with 3x3 array in it.这里graph<3,3>将是 class 类型的类型 ID,其中包含 3x3 数组。

  1. Use some container or other technique to store dynamic array at run-time.使用一些容器或其他技术在运行时存储动态数组。

You can use a std::vector<std::vector<bool>> and size the vector of vectors through the constructor which allows you to use runtime values for the number of rows and columns, eg您可以使用std::vector<std::vector<bool>>并通过构造函数调整向量向量的大小,这允许您使用运行时值来表示行数和列数,例如

#include <iostream>
#include <vector>

struct graph {
    std::vector<std::vector<bool>> v;
    graph (int n, int m) {
        static std::vector<std::vector<bool>> vb (n, std::vector<bool>(m));
        v = vb;
    }
    size_t get_n (void) { return v.size(); }
    size_t get_m (void) { return v[0].size(); }
};

This has the benefit of letting the std::vector container handle all memory management for you.这样做的好处是让std::vector容器为您处理所有 memory 管理。 You can decide whether you want a class or struct and write the additional setters and getters if you go the class route.如果您使用 go class 路线,您可以决定是否需要classstruct并编写额外的 setter 和 getter。 A short example showing the size of the std::vector<std::vector<bool>> set through the constructor would be:显示通过构造函数设置的std::vector<std::vector<bool>>大小的简短示例是:

int main (void) {
    
    graph g (4, 5);
    
    std::cout << g.get_n() << " x " << g.get_m() << '\n';
    
    for (auto n : g.v) {
        for (auto m : n)
            std::cout << " " << m;
        std::cout << '\n';
    }
}

Example Use/Output示例使用/输出

$ ./bin/graph2Dvect
4 x 5
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0

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

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