Vectors problem

Hi. English is not my native language so I'm sorry if im not correct. When i try to assign 48(ASCII=0)to plansza[0][0] and run the programme it crushes.... Dont know why.... And when i try to assign "x" it says: invalid conversion from 'const char*' to 'char' [-fpermissive]|
What's wrong ? :/
EDIT: Okay i found out it has to be 'x' to make it works properly, but still don't know why the programme crashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

using namespace std;
int n;
vector < vector <char> > plansza(n, vector <char> (n));




int main()
{
    cin >> n;
    plansza[0][0]=48;
    return 0;
}
Last edited on
Line 5: n is a global that defaults to 0.

Line 6: You're trying to allocate your vector of vectors with a size of 0 for each dimension.

Line 13: This has no affect on the size of your vectors. Your vectors have already been allocated with a size of 0.

Line 15: You can't reference the first entry in a vector whose size is 0. That's an out of bounds reference and is what is causing your program to crash.

And when i try to assign "x" it says: invalid conversion from 'const char*' to 'char' [-fpermissive]|

I don't see that in the code you posted.
Last edited on
How can i make this vector global and dependent on int n ?
You have two choices.
1) Line 5: Define n as a const and give it a value if you know the value at compile time.
2) Move line 6 to after line 13. Not global, but will use the entered value as the dimension.
Better style to not make it global.

I need to use it in few functions, i need it as a global, but user has to type it though.
Ok, i've done it in a different way. It will use more memory but who cares :D ( I don't ). Have a nice day. Thanks for the help :)
It will use more memory

I don't know what you've done, but if you define plansza as a local in main and pass it by reference to the functions that need it, it should not take any more memory assuming you pass by reference.

Topic archived. No new replies allowed.