[c++] 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

This question is related to c++ compiler-errors abstract-class

The answer is


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.


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.


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){};
}

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.

http://en.cppreference.com/w/cpp/language/override


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to compiler-errors

intellij idea - Error: java: invalid source release 1.9 Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug' Deserialize JSON with Jackson into Polymorphic Types - A Complete Example is giving me a compile error Android java.exe finished with non-zero exit value 1 error: expected primary-expression before ')' token (C) What does "collect2: error: ld returned 1 exit status" mean? Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing Maven error :Perhaps you are running on a JRE rather than a JDK? What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? Operator overloading ==, !=, Equals

Examples related to abstract-class

invalid new-expression of abstract class type Class is not abstract and does not override abstract method When to use: Java 8+ interface default method, vs. abstract method Spring can you autowire inside an abstract class? Abstract Class:-Real Time Example How should I have explained the difference between an Interface and an Abstract class? When do I have to use interfaces instead of abstract classes? Is it possible to make abstract classes in Python? Abstract Class vs Interface in C++ How do you handle a "cannot instantiate abstract class" error in C++?