2-dimensional vector (using STL)

K

Koen De Wolf

Dear all,

I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

Can someone point me in a direction to solve this problem? The source is
given below.

Many thanks,

Koen



/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;
public:
CFrame();
CFrame(int h,int w, int * buffer);
};

/* cpp file */

#include "CFrame.h"
#include <vector>
CFrame::CFrame(){
width = 0;
height = 0;
}
CFrame::CFrame(int width, int height, int * buffer){
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[j] = buffer [i * j + j];
}
}
 
J

Jon Bell

I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;

The vector Y has zero size, which means that you cannot use the []
operator either to retrieve or store data. Since it appears you know in
advance how much data is going to go into the vector, you should set its
size in the constructor before putting data into it.
public:
CFrame();
CFrame(int h,int w, int * buffer);
};
[snip]

Set the size in the constructor's initializer list.
CFrame::CFrame(int width, int height, int * buffer){

CFrame::CFrame (int width, int height, int * buffer)
: Y (height said:
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[j] = buffer [i * j + j];
}
}
 
K

Koen De Wolf

Hi Jon,

Thank you for your solution.

I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame.reserve(width);

But that didn't work out either.

kind regards,

Koen



Jon Bell said:
I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;

The vector Y has zero size, which means that you cannot use the []
operator either to retrieve or store data. Since it appears you know in
advance how much data is going to go into the vector, you should set its
size in the constructor before putting data into it.
public:
CFrame();
CFrame(int h,int w, int * buffer);
};
[snip]

Set the size in the constructor's initializer list.
CFrame::CFrame(int width, int height, int * buffer){

CFrame::CFrame (int width, int height, int * buffer)
: Y (height said:
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[j] = buffer [i * j + j];
}
}

 
K

Karl Heinz Buchegger

Koen said:

Please don't top post. Put your reply beneath the thing
you are replying to and snip away parts you don't noeed in your
reply. Thank you.
Thank you for your solution.

I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame.reserve(width);

But that didn't work out either.


You wan't resize, not reserve.
 
R

Rolf Magnus

Koen said:
I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame.reserve(width);

But that didn't work out either.


reserve only makes sure the vector has enough space for the specified
amout of elements, but doesn't create those elements. Use resize()
instead, which will default-initialize the elements.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top