您如何处理“无法实例化抽象类”? C ++中的错误? [英] How do you handle a "cannot instantiate abstract class" error in C++?

查看:416
本文介绍了您如何处理“无法实例化抽象类”? C ++中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何处理C ++中的无法实例化抽象类错误?
我已经看过这里的一些类似错误,但似乎没有一个与我完全相同或有问题。但是,我再次承认,还有几个要讨论的问题。这是编译错误:

How do you handle a "cannot instantiate abstract class" error in C++? I have looked at some of the similar errors here and none of them seem to be exactly the same or problem that I am having. But, then again, I will admit that there are several to go over. Here is the compile error:

这导致我转到此页面:
< href = http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(C2259);k(VS.ERRORLIST)&rd=true rel = noreferrer> http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(C2259); k(VS.ERRORLIST)& rd = true
编译错误C2259来自C ++程序,但是页面将​​抽象类称为接口:

This leads me to this page: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(C2259);k(VS.ERRORLIST)&rd=true Compile Error C2259 is from a C++ program but the page calls the abstract class an "interface":


无论何时如果您从一个接口派生并使用非public的访问权限在派生类中实现接口方法,则可能会收到C2259。发生这种情况是因为编译器希望在派生类中实现的接口方法具有公共访问权限。当您为具有更多限制性访问权限的接口实现成员函数时,编译器不会将它们视为接口中定义的接口方法的实现,从而使派生类成为抽象类。

Whenever you derive from an interface and implement the interface methods in the derived class with access permissions other than public, you may receive C2259. This occurs because the compiler expects the interface methods implemented in the derived class to have public access. When you implement the member functions for an interface with more restrictive access permissions, the compiler does not consider them to be implementations for the interface methods defined in the interface, which in turn makes the derived class an abstract class.

有两种方法可以解决该问题:

There are two possible workarounds for the problem:

将已实现方法的访问权限公开。

Make the access permissions public for the implemented methods.

将范围解析运算符用于派生类中实现的接口方法,以使用接口名称限定实现的方法名称。

Use the scope resolution operator for the interface methods implemented in the derived class to qualify the implemented method name with the name of the interface.

坏消息是我已经在该类中公开了所有方法:

The bad news is that I have already made all of the methods public in the class:

class AmbientOccluder: public Light {
    public:

        AmbientOccluder(void); 


推荐答案

该错误表示该类中有一些方法尚未实施。您无法实例化此类,因此除了实现该类方法的 all 之外,您无能为力。

The error means there are some methods of the class that aren't implemented. You cannot instantiate such a class, so there isn't anything you can do, other than implement all of the methods of the class.

另一方面,一种常见的模式是实例化一个具体的类,并将其分配给一个基本的基类的指针:

On the other hand, a common pattern is to instantiate a concrete class and assign it to a pointer of an abstrate base class:

class Abstract { /* stuff */ 4};
class Derived : virtual public Abstract { /* implement Abstract's methods */ };

Abstract* pAbs = new Derived; // OK

为避免上述行出现内存管理问题,您可以考虑使用智能指针,例如`std :: unique_ptr:

Just an aside, to avoid memory management issues with the above line, you could consider using a smart pointer, such as an `std::unique_ptr:

std::unique_ptr<Abstract> pAbs(new Derived);

这篇关于您如何处理“无法实例化抽象类”? C ++中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆