C++ 抽象类类型的无效新表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23827014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:33:19  来源:igfitidea点击:

invalid new-expression of abstract class type

c++compiler-errorsabstract-class

提问by hGen

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.

我目前正在为大学课程编写光线追踪器。为了从文件加载场景,我编写了一个 sdfloader 来读取 sdf 文件并为其创建场景。

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:

sdf_loader 类如下所示:

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():

在我的 sdf 加载器实现中,我编写了 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++0xto compile and link everything

我正在使用g++-4.9 - std=c++0x编译和链接所有内容

回答by nvoigt

invalid new-expression of abstract class type 'box'

抽象类类型“box”的无效新表达式

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

错误消息没有任何不清楚的地方。您的类box至少有一个未实现的成员,这意味着它是抽象的。您不能实例化抽象类。

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.

如果是设计使然,则从 box 派生,实现缺少的成员并使用派生类。

回答by B. Roberts

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.

如果您使用 C++11,您可以使用说明符“覆盖”,如果您没有正确覆盖抽象方法,它将给您一个编译器错误。您可能有一个与基类中的抽象方法不完全匹配的方法,因此您实际上并没有覆盖它。

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

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

回答by wacava

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

对于其他摸不着头脑的人,我遇到了这个错误,因为我对基类中的方法的参数之一进行了不恰当的 const 限定,因此派生类成员函数没有覆盖它。所以确保你没有类似的东西

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

回答by Jonathan Landrum

Another possible cause for future Googlers

未来 Google 员工的另一个可能原因

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

我遇到这个问题是因为我试图实现的方法需要 astd::unique_ptr<Queue>(myQueue)作为参数,但Queue该类是抽象的。我通过使用这样的QueuePtr(myQueue)构造函数解决了这个问题:

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 Queuewhen you make a std::unique_ptrof its type, which can't happen.

并在参数列表中使用它。这修复了它,因为初始化程序试图创建一个副本,Queue当您创建std::unique_ptr其类型时,这是不可能发生的。