Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kyo

vector1 = vector2 * 2.3 = debug assertion failed?

This topic is 5822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i''m testing my vector class and everything builds andcompiles but when i run it it says debug assertion failed. It only does this when i insert the line v1 = v2 * 3 which I overloaded with the following function:
  
inline CVector operator* (const CVector& vec,float s)
{
	return CVector(s*vec.v[0],s*vec.v[1],s*vec.v[2],s*vec.v[3]);
}
  
v1 = v2 works fine and so does v1 *= v2

Share this post


Link to post
Share on other sites
Advertisement
It seems like it does that debug assertion error only when i try to return a CVector into another CVector.


[edited by - Kyo on August 11, 2002 5:47:47 PM]

Share this post


Link to post
Share on other sites
I did i tried everything i could but i''m stumped. Here''s the class:


  
class CVector
{
public:
//constructors

CVector();
CVector(float a, float b, float c, float d);
~CVector();

//accessor functions

/*
const float& operator[] (int i) const { return v[i]; } //to read
float& operator() (int i) { return v[i]; } //to write
const float* ReadArray(void) { return v; }
float* GetArray(void) { return v; }*/


//private:

float v[3];

};



After creating the two CVectors


  
CVector vector2(0.4f,1.5f,-6.0f,1.0f);
CVector vector1(1.0f,1.0f,1.0f,1.0f);


I tested them the following way:

vector2 = vector1; <--works fine

vector1 = vector2 * 2; <--debug assertion error

vector1 * 3; <---same

vector2 *= 3; <--- works


I went through it line by line and it crashes when it tries to return a CVector in functions like these:


  
//--------------------------------------------------------

//vector times scalar (crashes after trying to return)

//--------------------------------------------------------

inline CVector operator* (const CVector& vec,float s)
{
return CVector(s*vec.v[0],s*vec.v[1],s*vec.v[2],s*vec.v[3]);
}



  
//--------------------------------------------------------

//reverse vector (crashes after trying to return

//--------------------------------------------------------

inline CVector operator- (const CVector& vec)
{
CVector tempvec(-vec.v[0],-vec.v[1],-vec.v[2],-vec.v[3]);
return tempvec;
}



  
//--------------------------------------------------------

//vector times equal scalar (works)

//--------------------------------------------------------

inline CVector& operator*= (CVector& vec,float s)
{
for (int i=0;i<4;i++)
{
vec.v[i] *= s;
}
return vec;
}


When it creates the vector using CVector(a,b,c,d) it goes through the constructor fine and gives the debug assertion error after it when it tries to return the vector. The constructor:


  
CVector::CVector(float a,float b,float c,float d)
{
v[0] = a;
v[1] = b;
v[2] = c;
v[3] = d;
}



I hope that''s enough information, it could be the operator overloading or use of consts as i''m not used to it fully yet.

Share this post


Link to post
Share on other sites
quote:
Original post by Kyo
float v[3];


You're declaring a 3-element array, but later you try to access v[3] (i.e. the 4th element). Change that declaration in the class to:

float v[4];


[edited by - Dactylos on August 11, 2002 8:09:55 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Dactylos
[quote]Original post by Kyo
float v[3];


You''re declaring a 3-element array, but later you try to access v[3] (i.e. the 4th element). Change that declaration in the class to:

float v[4];


[edited by - Dactylos on August 11, 2002 8:09:55 PM]

Nope still doesn''t work. Should''ve worked before too it went through the constructor giving v[3] a value without any problems.

Share this post


Link to post
Share on other sites
quote:
Original post by Kyo
Nope still doesn''t work. Should''ve worked before too it went through the constructor giving v[3] a value without any problems.

It doesn''t matter that it appeared to work, it was still wrong. It has to be v[4] if you want 4 elements.



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
It doesn''t matter that it appeared to work, it was still wrong. It has to be v[4] if you want 4 elements.



ok but as i said still doesn''t work.

Share this post


Link to post
Share on other sites
Try overloading operator=


University is a fountain of knowledge, and students go there to drink.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!