Home > front end >  Double declaration and initialization of vector in a class, C ?
Double declaration and initialization of vector in a class, C ?

Time:12-04

Good night dear all

I have a question, look i working with classes and in many cases i am using vectors of vector (2D vectors), my code runs quite well. However, i am a bit confused, look in my header file i declared a vector of vectors in my protected variables, then in my cpp file in the constructors section i declared again the vector of vectors, but this time giving the needed sizes and having "0" in all the elements. However, when i tried to use this vector of vectors in my member function, it seems that not dimension was delcared and not "0" values, if i use .size() the output is "0" and i was expecting 3.

However, when i declare again the vectors of vectors in the member (see commented line in the cpp file) function the code gives the 3 and the full matrix which is a 3 X 3, composed by "0".

Why is that? using the constructor is to bassically give the values of the variables.

See the next code, the commented line on the cpp file is on which i have declare again the vector.

the header file is:

#pragma once
#include <iostream>
#include <vector>

class Matrix
{
private:
    const int m_nRows;
    const int m_nCols;
protected:
    std::vector <std::vector <double>> MATRIX;

public:
    Matrix(int rows, int cols);
    ~Matrix();
    void getMatrix();
};

The cpp file is:

#include "Matrix.h"

Matrix::Matrix(int rows, int cols)
    : m_nRows(rows),
    m_nCols(cols)
{
    std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
}

Matrix::~Matrix()
{
}

void Matrix::getMatrix()
{
    //std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
    std::cout << MATRIX.size() << std::endl;
    for (auto& columns : MATRIX)
    {
        for (auto& element : columns)
        {
            std::cout << element << " ";
        }
        std::cout << "\n";
    }

}

The main file is:

#include <iostream>
#include <vector>
#include "Matrix.h"

    int main() {
        int rows = 3;
        int cols = 3;
    
        Matrix SmallMatrix(rows, cols);
        SmallMatrix.getMatrix();
    
        system("pause>0");
    }

CodePudding user response:

In your constructor:

Matrix::Matrix(int rows, int cols)
    : m_nRows(rows),
    m_nCols(cols)
{
    std::vector <std::vector <double>> MATRIX(m_nRows, std::vector<double>(m_nCols, 0));
}

you define a brand new variable with the name MATRIX, which is totally distinct from the member variable Matrix::MATRIX.

To initialize the Matrix::MATRIX member variable you should do it in the member initializer list, jut like for the m_nRows and m_nCols variables:

Matrix::Matrix(int rows, int cols)
    : m_nRows(rows),
    m_nCols(cols),
    MATRIX(m_nRows, std::vector<double>(m_nCols, 0))
{
}

CodePudding user response:

You are declaring another variable named MATRIX in the constructor. You have to instead use resize() on the MATRIX member declared in the class. The initialization of that MATRIX can be like this:

MATRIX.resize(m_nRows);
for (int i =0; i<m_nRows; i  ){
   MATRIX[i].resize(m_nCols, 0);
}
  • Related