类不存在默认构造函数

时间:2023-01-22
本文介绍了类不存在默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

#include "Includes.h"


enum BlowfishAlgorithm
    {
        ECB,
        CBC,
        CFB64,
        OFB64,
    };

class Blowfish
{
public:
    struct bf_key_st
    {
        unsigned long P[18];
        unsigned long S[1024];
    };
    Blowfish(BlowfishAlgorithm algorithm);
    void Dispose();
    void SetKey(unsigned char data[]);
    unsigned char Encrypt(unsigned char buffer[]);
    unsigned char Decrypt(unsigned char buffer[]);
    char EncryptIV();
    char DecryptIV();
private:
    BlowfishAlgorithm _algorithm;
    unsigned char _encryptIv[200];
    unsigned char _decryptIv[200];
    int _encryptNum;
    int _decryptNum;
};

class GameCryptography
{
public:
    Blowfish _blowfish;
    GameCryptography(unsigned char key[]);
    void Decrypt(unsigned char packet[]);
    void Encrypt(unsigned char packet[]);
    Blowfish Blowfish;
    void SetKey(unsigned char k[]);
    void SetIvs(unsigned char i1[],unsigned char i2[]);
};




GameCryptography::GameCryptography(unsigned char key[])
{
}

错误:智能感知:河豚"类不存在默认构造函数???!

Error:IntelliSense: no default constructor exists for class "Blowfish" ???!

推荐答案

如果你定义一个没有任何构造函数的类,编译器会为你合成一个构造函数(这将是一个默认的构造函数——也就是说,一个没有构造函数的构造函数)t 需要任何参数).但是,如果您确实定义了一个构造函数,(即使它确实需要一个或多个参数)编译器将为您合成一个构造函数——在这一点上,您已经负责构建该类的对象,因此编译器可以说退后一步",将这项工作留给您.

If you define a class without any constructor, the compiler will synthesize a constructor for you (and that will be a default constructor -- i.e., one that doesn't require any arguments). If, however, you do define a constructor, (even if it does take one or more arguments) the compiler will not synthesize a constructor for you -- at that point, you've taken responsibility for constructing objects of that class, so the compiler "steps back", so to speak, and leaves that job to you.

你有两个选择.您需要提供默认构造函数,或者在定义对象时需要提供正确的参数.例如,您可以将构造函数更改为如下所示:

You have two choices. You need to either provide a default constructor, or you need to supply the correct parameter when you define an object. For example, you could change your constructor to look something like:

Blowfish(BlowfishAlgorithm algorithm = CBC);

...因此可以在不(明确)指定算法的情况下调用构造函数(在这种情况下,它将使用 CBC 作为算法).

...so the ctor could be invoked without (explicitly) specifying an algorithm (in which case it would use CBC as the algorithm).

另一种选择是在定义 Blowfish 对象时明确指定算法:

The other alternative would be to explicitly specify the algorithm when you define a Blowfish object:

class GameCryptography { 
    Blowfish blowfish_;
public:
    GameCryptography() : blowfish_(ECB) {}
    // ...
};

在 C++ 11(或更高版本)中,您还有一个可用的选项.您可以定义带有参数的构造函数,然后告诉编译器生成如果您没有定义它的构造函数:

In C++ 11 (or later) you have one more option available. You can define your constructor that takes an argument, but then tell the compiler to generate the constructor it would have if you didn't define one:

class GameCryptography { 
public:

    // define our ctor that takes an argument
    GameCryptography(BlofishAlgorithm); 

    // Tell the compiler to do what it would have if we didn't define a ctor:
    GameCryptography() = default;
};

最后一点,我认为值得一提的是,ECB、CBC、CFB 等是操作模式,而不是真正的加密算法本身.称它们为算法不会打扰编译器,但很可能会给其他人阅读代码带来问题.

As a final note, I think it's worth mentioning that ECB, CBC, CFB, etc., are modes of operation, not really encryption algorithms themselves. Calling them algorithms won't bother the compiler, but is unreasonably likely to cause a problem for others reading the code.

这篇关于类不存在默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:C/C++ 结构与类 下一篇:带有 const 参数和重载的函数

相关文章

最新文章