Hello everyone,

I have used the Vector2 Class which is implemented by my asst. ,
and I encountered a problem with initializing it.

VS2008 kept saying I violate 0xC0000005 Error, and I counldn't find any reason for this.

Is there anyone who can tell me what mistakes I have made?

Thank you.

Hong

Vector2<float> m_pVec2Stage[150];
/*
...
*/

for(int n = 0; n<150; n++)
       m_pVec2Stage[n] = Vector2<float>(0,0);



/*
...
*/

template <typename T>
Vector2<T> &Vector2<T>::set (const Vector2 &d) {
	x = d[0];
	y = d[1];
	return (*this);
}

Recommended Answers

All 6 Replies

>>Vector2<float> m_pVec2Stage[150];
Most likely that line. vectors are by definition arrays. So all you probably need is this: Vector2<float> m_pVec2Stage; . But of course we can't be certain because we don't know how Vector2 was implemented.


The only reason I can think of to use the above construct is if yiou need a vector of vectors. In that case you would do this: Vector2< Vector2< float>> m_pVect2Stage;

The problem is it was ok when I used just one vector array like below

for(int n = 0 ; n<150; n++)
{
	m_pVec2Grid[n] = new Vector2<float>(0,0);
}

And As far as I know array can be made 2nd dim. , is it wrong?

Now you are using yet another new object that you have not told us about. What is m_pVect2Grid?? Please refrain from introducing new objects without defining them for use.

If you want a 2d vector you can do something like:

#include <iostream>
#include <vector> 

// These just make the notation used in the program easier
typedef std::vector<float> xVec;
typedef std::vector<xVec> xyVec;

int main( int argc, char *argv[] ) {
  // Define the x and y dimensions and the initial value
  int numY = 2;
  int numX = 4;
  float init = 9.94F;

  xyVec vec2d( numY, xVec(numX,init) );

  // Print the list
  for ( int y=0; y<numY; y++ ) {
    for ( int x=0; x<numX; x++ ) {
      std::cout<< "[" << y << "," << x << "] = " << vec2d[y][x] << "\t";
    }
    std::cout<< "\n";
  }

  return 0;
}

/* My output:
[0,0] = 9.94    [0,1] = 9.94    [0,2] = 9.94    [0,3] = 9.94
[1,0] = 9.94    [1,1] = 9.94    [1,2] = 9.94    [1,3] = 9.94
*/

Maybe y and x are reversed here.

when comming to the stl::vector , the use the at() method where
you need the bound checking. and note that [] operator does not
do the bound checking.

Hmm. I dunno.
I was demonstrating how to loop around safe bounds, not how to handle exceptions.
What's the point in throwing a throw if you're not gonna catch just for the throw when I know it's within bounds?
This thread is about arrays. Not exceptions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.