error: 'x' is not a type . Would anyone just debug it . I don't know what is wrong with it .

#include<iostream>
#include<vector>
using namespace std;

struct Matrix{
int x,y;
vector<vector<int> > X(x,vector<int>(y));

Matrix(int m,int n){
x=m;y=n;
}
};

int main(){
Matrix M(3,3);
}
1
2
3
4

Matrix(int m,int n)
: x(m),y(n) {}
vector<vector<int> > X(x,vector<int>(y));

This is not a real constructor. Maybe try

vector<vector<int> > X(x,vector<int>(y,0));

And since it’s a normal member variable, to should be initialized like the way markyrocks has done with the others.

1
2
Matrix(int m, int n)
: x(m), y(n), X(x,vector<int>(y,0)) {}


That might help..?
Last edited on
Lol I wasn't sure exactly which x he was talking about and I have no idea what is going on with those vectors.
He’s trying to make a (n)x(m) Matrix (so actually technically x and y could be dropped). The constructor overloads I provided are the fill constructors.
If it is n*m Matrix, then it would be better to use 1D storage internally and provide interface for accessing elements.
1
2
3
Matrix(int m, int n)
: rows(m), cols(n), X( rows*cols )
{}
Thanks guys @markyrocks @highwayman !
@keskiverto i didn't understand How could you simply put X(rows*cols) instead of X(rows,vector<int>(cols,0) )

By the way thanks for helping .

its just initializing a linear vector of ints but it still has the capacity of rows * cols so it can still be accessed in a similar way as a 2d array via the following. At least that would be my best guess.

1
2
3
4
5
6
7
8
9
	size_t row = 4; size_t cols = 5;
	vector<int> X(row * cols);

	for (size_t x = 0; x < row * cols; x++) {
		std::cout << X[x];
		if(x+1 >= cols && !((x + 1) % cols))// hmmmm . ... math is hard
			std::cout << "\n";
	}


probably alot easier then dealing with vectors inside of vectors
Last edited on
That math ...
1
2
3
4
5
6
for ( size_t row = 0; row < Rows ; ++row ) {
    for ( size_t col = 0; col < Cols; ++col ) {
        std::cout << X[ row * Cols + col ];
    }
    std::cout << '\n';
}


Built-in array do the same:
1
2
3
4
5
void func( int matrix[][Cols] ) {
  // code
  a = matrix[row][col]; // effectively does a = *(matrix + row*Cols + col);
  // code
}

That math ...


haha ya derr. I'll fix it so hopefully doesn't confuse. I just been at if for hrs. Brain is foggy.
Last edited on
Coool ! Thanks guys i got those mistakes && even found, far better ways of doing the same problem ..

Nice to meet you folks ! :)
Topic archived. No new replies allowed.