Reputation: 2285
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
Upvotes: 47
Views: 183388
Reputation: 151
If you use C++11, you can use the specifier "override", and it will give you a compiler error if you 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
Upvotes: 15
Reputation: 4262
This is an old question, but I landed here doing an upgrade/port of an old C++03 code base running on old hardware to C++20 on current hardware. This error appeared mysterious because the code in question compiled and ran successfully for many years on the old machine with the old compiler, while nothing in the standard related to this particular error seemed to have changed.
In my case, the class that complained derived, after multiple steps, from a third-party library's base class and the third-party library had added a virtual function to its class between revisions. In retrospect this makes sense, of course, but I lost some time on the red herring of possible differences in the standards or compilers. Hopefully this might answer save someone else some time.
Upvotes: 1
Reputation: 3172
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.
Upvotes: 6
Reputation: 491
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){};
}
Upvotes: 28
Reputation: 77304
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.
Upvotes: 74