C++ 错误是因为函数在类中是纯虚拟的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6375560/
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 20:00:22  来源:igfitidea点击:

Error because function is pure virtual within class?

c++classinheritance

提问by calccrypto

After copying over the code from Classes as parameter of function c++into my code, I am getting the error: note: because the following virtual functions are pure within 'TEA':and XTEA, but only those two functions. The other functions, AES, BLOWFISH, CAMELLIA, RC4, RC5, RC6, etc. are all working. Its just those two functions that are erroring. I don't get why.

类中的代码作为函数 c++ 的参数复制到我的代码中后,我收到错误:note: because the following virtual functions are pure within 'TEA':和 XTEA,但只有这两个函数。其他功能,AES、BLOWFISH、CAMELLIA、RC4、RC5、RC6 等都在工作。它只是出错的那两个函数。我不明白为什么。

code from link (slightly modified):

链接中的代码(稍作修改):

class CryptoAlgorithm
{
   public:
      virtual std::string encrypt(std::string DATA) = 0;
      virtual std::string decrypt(std::string DATA) = 0;
      virtual void setkey(std::string KEY) = 0;
};

and TEA setkey()

和茶 setkey()

void setkey(std::string KEY, 
            unsigned int ROUNDS = 64, 
            uint32_t DELTA = 0x9e3779b9, uint32_t TOTAL = 0xc6ef3720)

All of the functions in the other classes are the same. encrypt/decrypt only have std::string DATAas their arguments. void setkeyhas std::string KEYand other optional arguments. However, functions like RC6, whose setkeyalso has other optional arguments does not error.

其他类中的所有功能都是相同的。encrypt/decrypt 仅std::string DATA作为它们的参数。void setkeystd::string KEY和其他可选参数。但是,像 RC6 这样setkey还有其他可选参数的函数不会出错。

Any reason why?

有什么理由吗?

Also, all of the classes have : public CryptoAlgorithmnext to their declarations.

此外,所有类都: public CryptoAlgorithm在其声明旁边。

回答by Necrolis

The virtual function implementation' definitions must have the same protos as your pure virtual functions in the abstract class definition, else the compiler treats them as different functions(they become overloads), you are also required to implement every pure virtual function for the class not to be abstract(of course it can't define its own pure virtual functions either). The option to fix it is to have your realisation classes implement setKeywith only onestring argument(to match the virtual proto), then have a separate function for the overloads(this might lead to ambiguos calls from the default parameters however).

虚函数实现的定义必须与抽象类定义中的纯虚函数具有相同的原型,否则编译器会将它们视为不同的函数(它们变成重载),您还需要为类实现每个纯虚函数,而不是是抽象的(当然它也不能定义自己的纯虚函数)。修复它的选项是让您的实现类setKey仅使用一个字符串参数实现(以匹配虚拟原型),然后为重载提供一个单独的函数(但这可能会导致来自默认参数的模糊调用)。

回答by Alok Save

You cannot create objects of abstract classes. You need to ovverride the pure virtual functions in your derived class to be able to create objects of derived class.

您不能创建抽象类的对象。您需要覆盖派生类中的纯虚函数才能创建派生类的对象。

回答by Grammin

=0;is pure virtual in C++ which means that you have to implement those function in the child classes of CryptoAlgorithm.

=0;在 C++ 中是纯虚拟的,这意味着您必须在 CryptoAlgorithm 的子类中实现这些函数。

And as Als said, you cannot create objects from pure virtual (abstract) classes.

正如 Als 所说,你不能从纯虚拟(抽象)类创建对象。