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.
klax33Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
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.
0
yukapapaCommented:
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?
0
klax33Author Commented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

klax33Author Commented:
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?  
0
efnCommented:
It's possible that your problem is related to having code in a .h rather than .cpp file, but I don't think it's likely.

If you post the exact lines that are generating the errors, it might help us help you.

Another often helpful exercise is to try to make the smallest *compilable* program that demonstrates the problem.  Just throw out working stuff until you get to a minimal demonstration.  This exercise by itself will sometimes lead you to the solution.  If not, posting the minimal program here will give the experts a more complete problem statement to chew on.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yukapapaCommented:
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"
...


0
klax33Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.