Hello, I'm creating a Matrix, that the user can manually define the rows and columns.. (This will be defined in a class) But, I'm having trouble resizing the vector to what is set in main..

Here is the code:

Matrix.h

``````#ifndef _Matrix_h
#define _Matrix_h
#include <vector>

using namespace std;
class Matrix
{
public:

Matrix(); // Constructor
Matrix(double R, double C);
double getSize();

protected:
vector<vector<double> > matrix;

int rows;
int columns;
};
#endif``````

Matrix.cpp

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

using namespace std;

Matrix::Matrix(){};

Matrix::Matrix(double M, double N)
{
rows = M;
columns = N;

matrix.resize(rows);
matrix.resize(columns);

}

double Matrix::getSize()
{
return matrix.size();
}``````

main.cpp:

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

using namespace std;
int main()
{
Matrix m(100, 300);

cout << m.getSize();
return 0;
}``````

Any ideas? Thank you =)

## All 3 Replies

You need to resize the vector to M, then for each element in the vector, you need to resize each element to N, get it?

Ahh just confused me even more :P!

There are currently no elements inside the vector (If that makes sense?)

Shall I implement two for loops or something? :S

Sorry

>> There are currently no elements inside the vector (If that makes sense?)

Makes sense to me.

>> You need to resize the vector to M, then for each element in the vector, you need to resize each element to N, get it?

>> Shall I implement two for loops or something?

See red.

"The" -- singular
"two" -- plural.
"for each" -- Yes, use a for loop.

Use ONE for loop, not two, because you are traversing through ONE vector not two. For each INNER vector withing the OUTER vector, resize. Use ONE for loop to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.