Jump to content
  • Advertisement
Sign in to follow this  
Coluna

Problem with DevC++ and parameter as reference

This topic is 4316 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

Hello; Here is my code for a simple 3-float-vector class
class cVETOR3D  
{
public:
	cVETOR3D():x(0),y(0),z(0){}
	cVETOR3D(float xx,float yy,float zz):x(xx),y(yy),z(zz){}
	~cVETOR3D();

	//operações
	void operator =(cVETOR3D &vet)
	{	
		x = vet.x;
		y = vet.y;
		z = vet.z;
	}
	void operator +=(cVETOR3D &vet)
	{
		x+=vet.x;
		y+=vet.y;
		z+=vet.z;
	}
	void operator -=(cVETOR3D &vet)
	{
		x-=vet.x;
		y-=vet.y;
		z-=vet.z;
	}
	cVETOR3D operator +(cVETOR3D &vet)
	{
		cVETOR3D v;
		v.x = x+vet.x;
		v.y = y+vet.y;
		v.z = z+vet.z;
		return v;
	}
	cVETOR3D operator -(cVETOR3D &vet)
	{
		cVETOR3D v;
		v.x = x-vet.x;
		v.y = y-vet.y;
		v.z = z-vet.z;
		return v;
	}
	cVETOR3D operator*(float v)
	{
		return cVETOR3D(x*v,y*v,z*v);
	}
	cVETOR3D operator/(float v)
	{
		return cVETOR3D(x/v,y/v,z/v);
	}
	cVETOR3D operator*=(float v)
	{
		x*=v;
		y*=v;
		z*=v;
	}
	cVETOR3D operator/=(float v)
	{
		x/=v;
		y/=v;
		z/=v;
	}
	cVETOR3D operator-()
	{
		cVETOR3D v;
		v.x = -x;
		v.y = -y;
		v.z = -z;
		return v;
	}

	//retorna a magnitude do vetor
	float Magnitude()
	{
		return sqrtf(x*x+y*y+z*z);
	}

	//transforma o vetor para comprimento = 1
	void Normalizar()
	{
		float mag = Magnitude();
		//evita divisoes por 0
		if (mag == 0)
			mag = 1;
		x/=mag;
		y/=mag;
		z/=mag;
	}
	//retorna o produto interno entre 2 vetores
	float Dot(cVETOR3D &vet)
	{
		return x*vet.x+y*vet.y+z*vet.z;
	}

	//atribui um vetor resultante do produto cruzado entre v1 e v2
	void Cross(cVETOR3D &v1,cVETOR3D &v2)
	{
		x = v1.y * v2.z - v1.z*v2.y;
		y = v1.z * v2.x - v1.x*v2.z;
		z = v1.x * v2.y - v1.y*v2.x;
	}

	//reflete o vetor pela normal n								
	void Refletir(cVETOR3D &n)
	{
		cVETOR3D vn = n * (n.Dot(*this));
		cVETOR3D vt = *this - vn;
    	x = vt.x - vn.x;	
    	y = vt.y - vn.y;	
    	z = vt.z - vn.z;	
	}


	float x,y,z;
};

////////////////
//main code ...
////////////////
...
cVETOR3D posicao;
cVETOR3D direcao;
float velocidade;
posicao = posicao+direcao*velocidade;


this works ok in my VisualStudio, but in DevC++ it says no match for 'operator+' in 'posicao + cVETOR3D::operator*(float)' candidates are: cVETOR3D cVETOR3D::operator+(cVETOR3D&) So i think that i cannot have a method that use a reference for a returning instance (the method cVETOR3D*float return a local intance of cVETOR3D)...how can i do that work in DevC++? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
the way i have done it with devcpp is:

// deeclaration
p3d_type operator+(const p3d_type& addobject) const;

// definition
p3d_type p3d_type::operator+(const p3d_type& addobject) const
{
p3d_type temp;
temp.p_x = p_x + addobject.p_x;
temp.p_y = p_y + addobject.p_y;
temp.p_z = p_z + addobject.p_z;
return temp;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Coluna
No other solution?


the problem itself isn't specific to DevC++, rather it is specific to some versions of gcc, DevC++ only happens to come with such a flawed version of gcc.
In general, you should be able to address this issue by simply changing the signature of your methods to use const references, this should also improve the readability of your source code: http://www.parashift.com/c++-faq-lite/const-correctness.html

HTH

Share this post


Link to post
Share on other sites
I'm pretty sure it's expected that the assignment operators will return a reference to the object they modify.


FOO & FOO::operator =(const FOO &foo)
{
if(this != &foo)
{
// whatever
}
return *this;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!