vector of vector newbie problem

J

Jeff

Ive used straight vectors before, but never a vector of vectors
I realize this is a simple problem but for some reason I cant figure it
out.

Im getting a SEGV on the following line of code:
(I only put in relevant parts). x is defined as int *
and passed into the method as such.

typedef vector<vector< int * > > Item;
Item _item;
_item[0].push_back(x); <------SEGV line


However, if I use a single vector, it works fine:
typedef vector<int *> Item;
Item _item;
_item.push_back(x); <----- Ok

I realize this is probably a memory allocation issue on the vector of
vectors,
but why not on the single vector?

Secondly, how do I pre-allocate on the first example?

thanks for your help.
 
V

Victor Bazarov

Jeff said:
Ive used straight vectors before, but never a vector of vectors
I realize this is a simple problem but for some reason I cant figure it
out.

Im getting a SEGV on the following line of code:
(I only put in relevant parts). x is defined as int *
and passed into the method as such.

typedef vector<vector< int * > > Item;
Item _item;
_item[0].push_back(x); <------SEGV line

How many elements does '_item' have? You're addressing the 0th as if it
exists.

V
 
B

Ben Pope

Jeff said:
Ive used straight vectors before, but never a vector of vectors
I realize this is a simple problem but for some reason I cant figure it
out.

Im getting a SEGV on the following line of code:
(I only put in relevant parts). x is defined as int *
and passed into the method as such.

Hmm, I'm not sure I'd want it as a pointer. Have to be careful of who i
smanaging the lifetime.
typedef vector<vector< int * > > Item;
Item _item;
_item[0].push_back(x); <------SEGV line

_item[0] is empty!

You'll have to push a std::vector said:
However, if I use a single vector, it works fine:
typedef vector<int *> Item;
Item _item;
_item.push_back(x); <----- Ok

I realize this is probably a memory allocation issue on the vector of
vectors,
but why not on the single vector?

Hopefulyl the above comment makes sense.
Secondly, how do I pre-allocate on the first example?

If you want a vector of 8 vectors of 10 null pointers to int:

Item item_(8, std::vector<int*>(10, 0));

Ben Pope
 
J

Jim Langston

Jeff said:
Ive used straight vectors before, but never a vector of vectors
I realize this is a simple problem but for some reason I cant figure it
out.

Im getting a SEGV on the following line of code:
(I only put in relevant parts). x is defined as int *
and passed into the method as such.

typedef vector<vector< int * > > Item;
Item _item;
_item[0].push_back(x); <------SEGV line

_item is a vector of vectors, so it expects you to add vectors to it, yet
you're trying to add an int pointer.
Also, using an underscore for the first letter of a
variable/function/wheatever is supposed to be reserverd for the compiler,
you shouldn't do this.

This compiles:
int* x = new int;
*x = 10;
std::vector<std::vector< int* > > Item;
std::vector< int* > ThisItem;
ThisItem.push_back(x);
Item.push_back(ThisItem);
 
J

Jeff

Ok thank you for the comments. I tried this. This is my .h file which
declares
the vector of vectors:

#include <vector>
using namespace std;

typedef vector<vector< int * > > Item;

class Vec2
{
public:
Vec2() {};
~Vec2() {};

void add(int *);

private:
Item m_item(10,vector<int *>(5, 0)); <-----line 15
};

and I got the following compile error:

Vec2.h:15: error: expected identifier before numeric constant
Vec2.h:15: error: expected ',' or '...' before numeric constant

I just started doing QT and they seem to use _variable. I personally
like m_var instead.

also m_item is going to be dynamic and will grow depending on
how many items the user creates. How do I handle that? push_back()
will allocate more memory in each row if its greater than the
pre-allocated
amount (as I understand it), but what about adding additional rows
after
I define vector of vectors?

Thanks again
 
V

Victor Bazarov

Jeff said:
Ok thank you for the comments. I tried this. This is my .h file which
declares
the vector of vectors:

#include <vector>
using namespace std;

typedef vector<vector< int * > > Item;

class Vec2
{
public:
Vec2() {};
~Vec2() {};

void add(int *);

private:
Item m_item(10,vector<int *>(5, 0)); <-----line 15

This doesn't just declare it. This attempts to initialise it. You
cannot initialise non-static data members like that. It has to be done
in the constructor initialiser list. Read up on that.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top