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

查看:31
本文介绍了你如何处理“无法实例化抽象类"?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:

这将我带到此页面: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":

每当您从接口派生并在派生类中使用公共以外的访问权限实现接口方法时,您可能会收到 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); 

推荐答案

该错误表示该类的某些方法未实现.您无法实例化这样的类,因此除了实现该类的所有方法之外,您无能为力.

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天全站免登陆