krbu krbu - 11 months ago 58
C++ Question

No match for call to function C++

I'm trying to program a raytracer and I cant compile my program because of the following error:

src\util\Ray.cpp: In constructor 'Ray::Ray()':
src\util\Ray.cpp:8:17: error: no match for call to '(Vector3D) (double, double, double)'
o(0.0, 0.0, 0.0);
makefile.mak:31: recipe for target 'Ray.o' failed
mingw32-make: *** [Ray.o] Error 1

This is the code:

#ifndef __VECTOR3D__
#define __VECTOR3D__

class Vector3D{

float x;
float y;
float z;


Vector3D(const float&, const float&, const float&);
Vector3D(const Vector3D& obj);


#include <iostream>

#include "Vector3D.h"

using namespace std;

x = 0.0;
y = 0.0;
z = 0.0;

Vector3D::Vector3D(const float &p_x, const float &p_y, const float &p_z){
x = p_x;
y = p_y;
z = p_z;

Vector3D::Vector3D(const Vector3D& obj){
x = obj.x;
y = obj.y;
z = obj.z;

#ifndef __RAY__
#define __RAY__

#include "Vector3D.h"

class Ray{

Vector3D o;
Vector3D d;




#include "Ray.h"

o(0.0, 0.0, 0.0);

I cant figure out whats wrong here, could someone explain?

Answer Source

By the time you get to your constructor's body, all members have already been initialized. This means that in your Ray constructor:

    o(0.0, 0.0, 0.0);

the line o(0.0, 0.0, 0.0); is equivalent to o.operator()(0.0, 0.0, 0.0).

To call a non-default constructor for a member, you need to use an initialization list:

Ray::Ray() : o(0.0, 0.0, 0.0) {
    // note the body of the constructor is now empty