Problem with DevC++ and parameter as reference

Started by
3 comments, last by smart_idiot 17 years, 8 months ago
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!
Advertisement
the way i have done it with devcpp is:
// deeclarationp3d_type operator+(const p3d_type& addobject) const;// definitionp3d_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;}
No other solution?
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

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;   }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement