-
vectors and classes
Hey, I'm having some problems populating a two-dimensional vector that I defined in a class. I would like to do something like this:
Code:
class A {
public:
A(); // constructor
print_A(); // print function
private:
vector<vector<char> > a_vector;
}; // A
A::A() {
a_vector.resize(8,8); // re-size the vector to an 8x8 grid
}
This is just a brief example of what I'm trying to do, but when I try to compile my code, it gives me an error in the constructor: "sorry, not implemented: 'overload not supported by dump_expr'". I'm not sure if this is a problem with my compilier (bloodshed dev-c++ version 4, which was the last stable release), or just a quirk in the code.
Also, I am planning on using several classes in my program that all use the same vector. Would pointers be the best way to access\change the data stored in the vector?
Thanks.
jim
-
>>a_vector.resize( 8, 8 );
>>void resize( size_type size, TYPE val );
This will NOT resize the vector to an 8x8 matrix. It will try to create 8 'vector< char >( 8 )'s, which will return an error. Instead, use this.
Code:
for( int i = 0; i < 8; i++ ) {
a_vector.push_back( vector< char >( 8, '\0' ) );
}
-
works!
works great, thank you very much. Just a question though, what does the '\0' do? Does that just insert a null value to the cells?
Cheers,
Jim
-
Yes. The second parameter ('\0') is the value to initalise the chars to.