astrovarun
astrovarun

Reputation: 1

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? what is the meaning of this error?

Upvotes: 0

Views: 369

Answers (4)

David C. Rankin
David C. Rankin

Reputation: 84561

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, e.g.

#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. You can decide whether you want a class or struct and write the additional setters and getters if you go the class route. A short example showing the size of the std::vector<std::vector<bool>> set through the constructor would be:

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

Upvotes: 0

I S
I S

Reputation: 456

You cannot declare variable size array. bool matrix[N][M] size should be determined at compile time. 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;
    }
};

Upvotes: 2

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14589

You have two options.

  1. Decided that N and M are compile-time constants and make your class a template.

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.

  1. Use some container or other technique to store dynamic array at run-time.

Upvotes: 1

Wielki_i_niezlomny
Wielki_i_niezlomny

Reputation: 131

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.

Upvotes: 4

Related Questions