7 Replies - 1004 Views - Last Post: 26 December 2013 - 11:51 AM Rate Topic: -----

#1 LiteWarfare   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 13

Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:03 AM

I am trying to set a float variable (m_x) equal to another float (x) but get an access violation writing location error. When it comes to float numbers I am quite new to them, so I am not sure how to fix it.

#ifndef _VECTO2D_H
#define _VECTOR2D_H

#include "UniversalInclude.h" //Includes iostream
#include <math.h>

class Vector2D
{
public:
	float m_x;
	float m_y;
	
	Vector2D()
	{
		m_x = 0;
		m_y = 0;
	}

	Vector2D(float x, float y) : m_x(x), m_y(y) {}

	const float getX()
	{ 
		return m_x;
	}
	const float getY()
	{ 
		return m_y; 
	}

	void setX(float x)
	{ 
		m_x = x;		//Error occurs here
	}
	void setY(float y) 
	{ 
		m_y = y; 
	}

	float length() { return sqrt(m_x * m_x + m_y * m_y); }

	//Addition of vectors
	Vector2D operator+(const Vector2D& v2) const
	{
		return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
	}
	friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2)
	{
		v1.m_x += v2.m_x;
		v1.m_y += v2.m_y;

		return v1;
	}
	//--------------------
	//Subtraction of Vectors
	Vector2D operator-(const Vector2D& v2) const
	{
		return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
	}
	friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2)
	{
		v1.m_x -= v2.m_x;
		v1.m_y -= v2.m_y;

		return v1;
	}
	//--------------------
	//Scalar of vectors
	Vector2D operator*(float scalar)
	{
		return Vector2D(m_x * scalar, m_y * scalar);
	}

	Vector2D& operator*=(float scalar)
	{
		m_x *= scalar;
		m_y *= scalar;

		return *this;
	}
	//--------------------
	//Division by scalar number
	Vector2D operator/(float scalar)
	{
		return Vector2D(m_x / scalar, m_y / scalar);
	}

	Vector2D& operator/=(float scalar)
	{
		m_x /= scalar;
		m_y /= scalar;

		return *this;
	}
	//--------------------
	//Normalizing Vectors (making length of vector 1 unit)
	void normalize()
	{
		float l = length();
		if (l > 0)
		{
			(*this) *= 1 / 1;
		}
	}
};

#endif /* Defined (_VECTOR2D_H) */



Is This A Good Question/Topic? 0
  • +

Replies To: Access violation when setting a float equal to another float

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,833
  • Joined: 08-August 08

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:11 AM

Post the smallest yet complete set of code that has this problem.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:18 AM

Your include guard is incorrect, I think it was a typo.
Was This Post Helpful? 0
  • +
  • -

#4 LiteWarfare   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 13

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:23 AM

Ok, here's the area where the error occurs: (This includes the definitions of the variables used in the error)

#include "UniversalInclude.h" //Includes iostream
#include <math.h>

class Vector2D
{
public:
	float m_x;
	float m_y;
	
	Vector2D()
	{
		m_x = 0;
		m_y = 0;
	}

	Vector2D(float x, float y) : m_x(x), m_y(y) {}

	const float getX()
	{ 
		return m_x;
	}
	const float getY()
	{ 
		return m_y; 
	}

	void setX(float x)
	{ 
		m_x = x;		//Error occurs here
	}
	void setY(float y) 
	{ 
		m_y = y; 
	}



Yes the include guard was a typo, it is fixed now.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,833
  • Joined: 08-August 08

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:25 AM

I don't understand why you would think we can copy/paste that into a new project and test it.
Was This Post Helpful? 0
  • +
  • -

#6 LiteWarfare   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 13

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 09:44 AM

I dug a bit deeper and found that the program is "unable to read memory" of (m_x) and (m_y)
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 11:06 AM

We can't tell what the problem is until we see a main() function that uses the above class or should I say probably misuses the class.

That's why CTphpnwb asked for the "smallest but complete program illustrating your problem". The code you posted is not complete.
Was This Post Helpful? 0
  • +
  • -

#8 LiteWarfare   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-February 13

Re: Access violation when setting a float equal to another float

Posted 26 December 2013 - 11:51 AM

I found what caused the error. It was a function which did not change the variable's values correctly. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1