invalid new-expression of abstract class type

I am currently writing a raytracer for a university class. In order to load Scenes from files I wrote an sdfloader to read sdf files and create scenes therefor.

if I now want to compile the loader i get the following error:

rc/sdf_loader.cpp: In member function 'void SDFloader::add_shape(std::istringstream&)': src/sdf_loader.cpp:95:58: error: invalid new-expression of abstract class type 'box' &scene_.materials[mat])); ^ 

I tried to find a solution but failed.

The sdf_loader class looks like the following:

class SDFloader { public: SDFloader(); ~SDFloader(); Scene const& scene() const; void read(std::string file); private: void add_material(std::istringstream&); void add_shape(std::istringstream&); void add_light(std::istringstream&); void add_camera(std::istringstream&); void apply_transformation(std::istringstream&); private: Scene scene_; }; 

in my implementation of the sdf loader i wrote the method read():

void SDFloader::add_shape(std::istringstream& iss) { std::string name; iss >> name; if(name == "box") { double x1,y1,z1,x2,y2,z2; std::string mat; iss >> name >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> mat; scene_.shapes.insert(new box(point(x1,y1,z1), point(x2,y2,z2), name, &scene_.materials[mat])); } 

and for every other shape the same calls.

Where is the Problem in my code? I really don't see it

I am using g++-4.9 - std=c++0x to compile and link everything

2

4 Answers

invalid new-expression of abstract class type 'box'

There is nothing unclear about the error message. Your class box has at least one member that is not implemented, which means it is abstract. You cannot instantiate an abstract class.

If this is a bug, fix your box class by implementing the missing member(s).

If it's by design, derive from box, implement the missing member(s) and use the derived class.

3

for others scratching their heads, I came across this error because I had innapropriately const-qualified one of the arguments to a method in a base class, so the derived class member functions were not over-riding it. so make sure you don't have something like

class Base { public: virtual void foo(int a, const int b) = 0; } class D: public Base { public: void foo(int a, int b){}; } 
1

If you use C++11, you can use the specifier "override", and it will give you a compiler error if your aren't correctly overriding an abstract method. You probably have a method that doesn't match exactly with an abstract method in the base class, so your aren't actually overriding it.

Another possible cause for future Googlers

I had this issue because a method I was trying to implement required a std::unique_ptr<Queue>(myQueue) as a parameter, but the Queue class is abstract. I solved that by using a QueuePtr(myQueue) constructor like so:

using QueuePtr = std::unique_ptr<Queue>; 

and used that in the parameter list instead. This fixes it because the initializer tries to create a copy of Queue when you make a std::unique_ptr of its type, which can't happen.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like