Link to home
Start Free TrialLog in
Avatar of klax33
klax33

asked on

C++ STL Vectors

I'm trying to declare a 2D array using vectors and have:

#include <vector>

vector< vector<int> > V(3, vector<int> (3));

V[0][0] = 1;

However that generates the following errors:
error C2466: cannot allocate an array of constant size 0
error C2466: cannot allocate an array of constant size 0
error C2087: '<Unknown>' : missing subscript
error C2501: 'V' : missing storage-class or type specifiers
error C2440: 'initializing' : cannot convert from 'const int' to 'int [][1]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays

Can you not reference vectors using the same notation as 2D arrays?

Thanks.
Avatar of efn
efn

Your code looks OK to me and Microsoft Visual C++ 6.0 compiled it without complaint.  Maybe the problem lies elsewhere?

To answer your question, yes, you should be able to reference an element in a vector of vectors with the same notation as for a two-dimensional array.
Hi, klax33.

I could not reproduce the problem with following code:

#include <vector>

int main(){
std::vector< std::vector<int> > V(3, std::vector<int>(3));
V[0][0] = 1;
return 0;
}

I compiled this with Visual C/C++ .Net.

You know the template class vector is defined in the namespace std, don't you?
Avatar of klax33

ASKER

Thank you all for your comments.  I tried adding both:

using std::vector;

and

using namespace std;

to the top of the file, without success.  The errors still show up on compile.  Well at least I know I wasn't going crazy. :)  I'll work a little longer with it and post an update.
Avatar of klax33

ASKER

After a reboot, I tried using your code yukapapa and it compiled, built, and ran correctly.  I do not know why mine does not work.  Could it be because I'm trying to do this in a *.h instead of a *.cpp file?  
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you put the definition of the variable V in your header(*.h) file, V can be defined multiple times as follows:

---A.h---
...
std::vector< std::vector<int> > V(3, std::vector<int>(3));
...

---B.cpp---
...
#include "A.h"
...

--C.cpp--
...
#include "A.h"
...

If your project is composed by 2 source files B.cpp and C.cpp both include a header file A.h then the variable V defined 2 times. The effect is just as follows:

---B.cpp---
...
std::vector< std::vector<int> > V(3, std::vector<int>(3));
...

--C.cpp--
...
std::vector< std::vector<int> > V(3, std::vector<int>(3));
...

And the V in B.cpp and in C.cpp is diferent. For example, if you change the value V[0][0] in the B.cpp, the value of V[0][0] in the C.cpp is never changed. I think this is not what you want.

If you want to define the variable to use in multiple source files(ie as global variable), you can put the declaration(not definition) for the variable in your header file, and the definition in a source file as follows:

---A.h---
...
#include <std::vector>

//V's declaration used by multiple source files.
//extern indicates the variable V is defined in somewhere.
extern std::vector< std::vector<int> > V;
...

---B.cpp---
...
#include "A.h"

//V's definition.
extern std::vector< std::vector<int> > V(3, std::vector<int>(3));

--C.cpp--
...
#include "A.h"
...


Avatar of klax33

ASKER

V[0][0] = 1;  is the line that is causing all of the errors.  I tried moving the offending code into the *.cpp file and it produced the same error.  I created a new project using the same code (copy and paste) and it did not work when the offending line was outside of main, but it did work when it was put inside of main.  I tried the same idea using int's and had the same result.  I don't know why I thought you could assign variables outside of functions and such.  I guess I'll just have to create functions to assign all the values to my vectors.  Thank you very much for your problem-solving idea efn, it indeed led me to the answer.  Thank you too yukapapa for your detailed help too.