Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 914
hGen Avatar asked May 23 '14 10:05

hGen


3 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.

like image 72
nvoigt Avatar answered Oct 21 '22 23:10

nvoigt


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){};
}
like image 25
wacava Avatar answered Oct 21 '22 22:10

wacava


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

like image 13
B. Roberts Avatar answered Oct 21 '22 23:10

B. Roberts