# Problem with DevC++ and parameter as reference

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

## 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;


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 on other sites
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;}

##### Share on other sites
No other solution?

##### Share on other sites
Quote:
 Original post by ColunaNo 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 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;   }

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015564
×