Long time since I coded in C++ and I'm getting some error I can't solve:
In the line (take a look at the code snippets): list.push_back(shape)
error: invalid new-expression of abstract class type 'Shape'
But I'm also a bit confused because a bit further up in the compilation log I see:
note: 'virtual bool Shape::hit(const Ray&, float, float, hitRecord&) const' virtual bool hit(const Ray& ray, float tMin, float tMax, hitRecord& hit) const = 0;
I have this abstract class:
#pragma once
#include "Ray.h"
struct hitRecord
{
float t;
Vec3 p;
Vec3 normal;
};
class Shape
{
public:
virtual bool hit(const Ray& ray, float tMin, float tMax, hitRecord& hit) const = 0;
};
And then two children classes which implement hit:
#pragma once
#include "Shape.h"
class Sphere: public Shape
{
public:
Vec3 center{};
float radius{};
Sphere() = default;
virtual bool hit(const Ray& ray, float tMin, float tMax, hitRecord& hit) const
{
//Do stuff
return false;
}
};
And:
#pragma once
#include "Shape.h"
#include <vector>
class ShapeList: public Shape
{
public:
std::vector<Shape> list;
ShapeList()
{
list = std::vector<Shape>();
};
virtual bool hit(const Ray& ray, float tMin, float tMax, hitRecord& hit) const
{
// Do Stuff
return hitAnything;
}
void append(Shape& shape)
{
list.push_back(shape);
}
};
Finally in main() I'm basically doing
ShapeList world;
Sphere sphere();
world.append(sphere1);