?
Solved

C++ STL Vectors

Posted on 2003-03-16
7
Medium Priority
?
510 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:klax33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:efn
ID: 8148310
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
 
LVL 1

Expert Comment

by:yukapapa
ID: 8148314
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
 

Author Comment

by:klax33
ID: 8148362
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:klax33
ID: 8148483
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
 
LVL 15

Accepted Solution

by:
efn earned 200 total points
ID: 8148659
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
 
LVL 1

Expert Comment

by:yukapapa
ID: 8148665
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
 

Author Comment

by:klax33
ID: 8148709
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question