C++: Default constructor throws error when adding members to class

  c++, constructor, default, initialization, member

I am trying to understand some code right now and I’ve stumbled upon something that I don’t really understand regarding the constructors of the following class:

intersection.cpp:

namespace rt {

Intersection::Intersection(float distance, const Ray& ray, const Solid* solid, const Vector& normal, const Point& local)
    : distance(distance), ray(ray), solid(solid) { }

    
Intersection Intersection::failure() {
    return Intersection(INFINITY, Ray(), nullptr, Vector(), Point()); 
}
}

intersection.h:

namespace rt {

class Solid;

class Intersection {
public:
    Ray ray;
    const Solid* solid;
    float distance;

    Intersection() {}
    static Intersection failure();
    Intersection(float distance, const Ray& ray, const Solid* solid, const Vector& normal, const Point& uv);
}

Now as you can see, the class has three members ray, solid and distance.
If I want to add the parameters that are passed in the third constructor normal and uv to the class as members, the default constructor does not work anymore despite the classes Vector and Point also having default constructors. It says that the constructor must explicitly initialize the respective member. Why does this work with the existing fields?

Thanks in advance.

Source: Windows Questions C++

LEAVE A COMMENT