Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i initialize a bi-dimensional vector in a class in C++?

Since i need to define its size in runtime, i need a vector. I tried the following but i get a compiling error: "error: ‘V’ is not a type"

#include<iostream>
#include<vector>

class graph {
private:
    int V;
    std::vector<int> row(V);
    std::vector<std::vector<int>> matrix(V,row);
public:
    graph(int v): V(v) {}
};

Is there something i am failing to understand? is it even allowed to initialize a vector this way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The compiler considers these lines

std::vector<int> row(V);
std::vector<std::vector<int>> matrix(V,row);

as member function declarations with parameters with omitted type specifiers.

It seems what you need is the following

class graph {
private:
    int V;
    std::vector<std::vector<int>> matrix;
public:
    graph(int v): V(v),  matrix( v, std::vector<int>( v ) ) {}
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading