By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

2-dimensional vector (using STL)

P: n/a
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[i][j] = buffer [i * j + j];
}
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <bq**********@gaudi2.UGent.be>,
Koen De Wolf <Ko**@raadselweb.net> wrote:

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, vector<int>(width)) {
this->width = width;
this->height = height;

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


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2

P: n/a
Hi Jon,

Thank you for your solution.

I also tried this:

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

But that didn't work out either.

kind regards,

Koen

"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
In article <bq**********@gaudi2.UGent.be>,
Koen De Wolf <Ko**@raadselweb.net> wrote:

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, vector<int>(width)) {
this->width = width;
this->height = height;

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


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Jul 22 '05 #3

P: n/a
Koen De Wolf wrote:

Hi Jon,
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[i].reserve(width);

But that didn't work out either.


You wan't resize, not reserve.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a
Koen De Wolf wrote:
I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame[i].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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.