vector1 = vector2 * 2.3 = debug assertion failed?

Started by
17 comments, last by Kyo 21 years, 9 months ago
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
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]
Why not step into the code when it gives you the error and find the exact line it crashes on? And by that, I mean the exact line it executes before it gives you the error. That should give you what you need to know.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
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; }	//to read<br>	float& operator() (int i) { return v; }				//to write<br>	const float* ReadArray(void) { return v; }				<br>	float* GetArray(void) { return v; }*/</font><br><br><font color="gray">//private:<br></font><br>	<font color="blue">float</font> v[<font color="purple">3</font>];<br><br>};<br><br>  </pre></DIV><!–ENDSCRIPT–><br><br> After creating the two CVectors <br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br>CVector vector2(0.4f,1.5f,-6.0f,1.0f);<br>CVector vector1(1.0f,1.0f,1.0f,1.0f);<br>  </pre></DIV><!–ENDSCRIPT–><br><br>I tested them the following way:<br><br>vector2 = vector1;  <–works fine<br><br>vector1 = vector2 * 2;  <–debug assertion error<br><br>vector1 * 3;    <—same<br><br>vector2 *= 3;  <— works<br><br><br> I went through it line by line and it crashes when it tries to return a CVector in functions like these: <br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="gray">//vector times scalar (crashes after trying to return)<br></font><br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="blue">inline</font> CVector operator* (const CVector& vec,<font color="blue">float</font> s)<br>{<br>	<font color="blue">return</font> CVector(s*vec.v[<font color="purple">0</font>],s*vec.v[<font color="purple">1</font>],s*vec.v[<font color="purple">2</font>],s*vec.v[<font color="purple">3</font>]);<br>}<br>  </pre></DIV><!–ENDSCRIPT–><br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="gray">//reverse vector (crashes after trying to return<br></font><br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="blue">inline</font> CVector operator- (const CVector& vec)<br>{<br>	CVector tempvec(-vec.v[<font color="purple">0</font>],-vec.v[<font color="purple">1</font>],-vec.v[<font color="purple">2</font>],-vec.v[<font color="purple">3</font>]);<br>	<font color="blue">return</font> tempvec;<br>}<br>  </pre></DIV><!–ENDSCRIPT–><br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="gray">//vector times equal scalar (works)<br></font><br><font color="gray"><font color="gray">//——————————————————–<br></font></font><br><font color="blue">inline</font> CVector& operator*= (CVector& vec,<font color="blue">float</font> s)<br>{<br>	<font color="blue">for</font> (<font color="blue">int</font> i=0;i&lt;4;i++)<br>	{<br>		vec.v[<font color="purple">i</font>] *= s;<br>	}<br>	<font color="blue">return</font> vec;<br>}<br>  </pre></DIV><!–ENDSCRIPT–><br><br> 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:<br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br>CVector::CVector(<font color="blue">float</font> a,<font color="blue">float</font> b,<font color="blue">float</font> c,<font color="blue">float</font> d)<br>{<br>	v[<font color="purple">0</font>] = a;<br>	v[<font color="purple">1</font>] = b;<br>	v[<font color="purple">2</font>] = c;<br>	v[<font color="purple">3</font>] = d;<br>}<br>  </pre></DIV><!–ENDSCRIPT–><br><br><br> 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. <br><br>    
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]
quote:Original post by Dactylos
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.
Anyone please? I can''t really do anything else till i get this fixed
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 ]
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.

Try overloading operator=


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

This topic is closed to new replies.

Advertisement