"no matching function for call to"?

L

Lateralus

headers/vector3.h: In member function ‘Vector3
Vector3::eek:perator*(Scalar)’:
headers/vector3.h:13: error: no matching function for call to
‘Vector3::Vector3(Vector3)’
‘Vector3::Vector3(Vector3)’
headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&)

I'm a little confused as to what my problem is. I get this error for
any method which calls the constructor.

#ifndef __VECTOR3_H__
#define __VECTOR3_H__
#include "common_math.h"

class Vector3 {
public:
Scalar x, y, z;
Vector3(void) { x = y = z = 0; }
Vector3(Scalar fx, Scalar fy, Scalar fz) { x = fx; y = fy; z = fz; }
Vector3(Vector3& v) { x = v.x; y = v.y; z = v.z; }

Vector3 operator=(Vector3& v) { x = v.x; y = v.y; z = v.z; return v;}
Vector3 operator*(Scalar s) { return Vector3(x*s, y*s, z*s); }
friend Vector3 operator*(Scalar s, Vector3& v) { return
Vector3(v.x*s, v.y*s, v.z*s); }
Vector3 operator/(Scalar s) { return Vector3(x/s, y/s, z/s); }
Vector3 operator+(Vector3& v) { return Vector3(x+v.x, y+v.y, z+v.z);
}
Vector3 operator-(Vector3& v) { return Vector3(x-v.x, y-v.y, z-v.z);
}
void operator+=(Vector3& v) { x += v.x; y += v.y; z += v.z; }
void operator-=(Vector3& v) { x -= v.x; y -= v.y; z -= v.z; }
void operator*=(Scalar s) { x *= s; y *= s; z *= s; }
void operator/=(Scalar s) { x /= s; y /= s; z /= s; }

Scalar Magnitude(void) { return (Scalar)sqrt(x*x + y*y + z*z); }
Vector3 Normalize(void) { return *this/Magnitude(); }

Scalar DotProduct(Vector3& v) { return (x*v.x + y*v.y + z*v.z); }
Vector3 CrossProduct(Vector3& v) { return Vector3((y*v.z)-(z*v.y),
(z*v.x)-(x*v.z), (x*v.y)-(y*v.x)); }

Vector3 Rotate(Scalar xr, Scalar yr, Scalar zr) {
Scalar sxr, syr, szr, cxr, cyr, czr;
sxr = (Scalar)sin(DegToRad(xr));
syr = (Scalar)sin(DegToRad(yr));
szr = (Scalar)sin(DegToRad(zr));
cxr = (Scalar)cos(DegToRad(xr));
cyr = (Scalar)cos(DegToRad(yr));
czr = (Scalar)cos(DegToRad(zr));
return Vector3(((x*(cyr*czr))+
(y*(cyr*szr))+
(z*(-syr))),
((x*((sxr*syr*czr)+(cxr*(-szr))))+
(y*((sxr*syr*szr)+(cxr*czr)))+
(z*(sxr*cyr))),
((x*((cxr*syr*czr)+(sxr*(-szr))))+
(y*((cxr*syr*szr)+((-sxr)*czr)))+
(z*(cxr*cyr))));
}
};

#endif //__VECTOR3_H__
 
A

Alf P. Steinbach

* Lateralus:
headers/vector3.h: In member function =E2=80=98Vector3
Vector3::eek:perator*(Scalar)=E2=80=99:
headers/vector3.h:13: error: no matching function for call to
=E2=80=98Vector3::Vector3(Vector3)=E2=80=99
=E2=80=98Vector3::Vector3(Vector3)=E2=80=99
headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&)

Read up on 'const'.
 
O

Old Wolf

Lateralus said:
headers/vector3.h: In member function 'Vector3
Vector3::eek:perator*(Scalar)':
headers/vector3.h:13: error: no matching function for call to
'Vector3::Vector3(Vector3)'
'Vector3::Vector3(Vector3)'
headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&)

I'm a little confused as to what my problem is. I get this error for
any method which calls the constructor.

When you "call a constructor" in C++, you are actually creating
a new object. It's good to avoid that expression, because it can
cause confusion when people have come from other languages
(eg. Java, C#) where the same syntax will actually call a function
for the existing object (that function being the constructor).
#ifndef __VECTOR3_H__
#define __VECTOR3_H__
#include "common_math.h"

class Vector3 {
public:
Scalar x, y, z;
Vector3(void) { x = y = z = 0; }
Vector3(Scalar fx, Scalar fy, Scalar fz) { x = fx; y = fy; z = fz; }
Vector3(Vector3& v) { x = v.x; y = v.y; z = v.z; }

To fix your problem, change this to:

Vector3(Vector3 const & v) { x = v.x; y = v.y; z = v.z; }

This indicates that the constructor does not modify 'v'.

Do the same thing for all of your other functions that take
parameters by reference (unless you do actually want to
modify the parameter).
Vector3 operator=(Vector3& v) { x = v.x; y = v.y; z = v.z; return v;}
Vector3 operator*(Scalar s) { return Vector3(x*s, y*s, z*s); }

What happens here is that " Vector3(x*s, y*s, z*s) " creates
an un-named (aka. temporary) Vector object, local to this function.
Then the "return" expression copies this local object into wherever
the return value is supposed to go.

To copy objects in C++ you need a copy-constructor.
The parameter to this copy-constructor will be the temporary
Vector3 object.

But C++ has a rule that a non-const reference cannot be bound
to a temporary object (for rationale on this, search the NG
for "non-const reference temporary").

So the compiler cannot find any copy-constructor that will
accept a temporary Vector3 as an argument, so it fails to compile.

Note: Some compilers (eg. Visual C++ 6.0) violate this rule.
Scalar Magnitude(void) { return (Scalar)sqrt(x*x + y*y + z*z); }

Vector3 Rotate(Scalar xr, Scalar yr, Scalar zr) {
Scalar sxr, syr, szr, cxr, cyr, czr;
sxr = (Scalar)sin(DegToRad(xr));
syr = (Scalar)sin(DegToRad(yr));
szr = (Scalar)sin(DegToRad(zr));
cxr = (Scalar)cos(DegToRad(xr));
cyr = (Scalar)cos(DegToRad(yr));
czr = (Scalar)cos(DegToRad(zr));

These casts are all useless (assuming Scalar doesn't have
a constructor declared 'explicit'). It's good to avoid casts
if they are not necessary, as it makes other developers
wonder why you did it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top