简体   繁体   English

类不存在默认构造函数

[英]no default constructor exists for class

#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" ???!错误:智能感知:“河豚”类不存在默认构造函数???!

If you define a class without any constructor, the compiler will synthesize a constructor for you (and that will be a default constructor -- ie, 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);

...so the ctor could be invoked without (explicitly) specifying an algorithm (in which case it would use CBC as the algorithm). ...因此可以在不(明确)指定算法的情况下调用 ctor(在这种情况下,它将使用 CBC 作为算法)。

The other alternative would be to explicitly specify the algorithm when you define a Blowfish object:另一种选择是在定义 Blowfish 对象时明确指定算法:

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

In C++ 11 (or later) you have one more option available.在 C++ 11(或更高版本)中,您还有一个可用选项。 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;
};

As a final note, I think it's worth mentioning that ECB, CBC, CFB, etc., are modes of operation, not really encryption algorithms themselves.最后,我认为值得一提的是,ECB、CBC、CFB 等是操作模式,而不是真正的加密算法本身。 Calling them algorithms won't bother the compiler, but is unreasonably likely to cause a problem for others reading the code.将它们称为算法不会打扰编译器,但可能会给阅读代码的其他人带来问题。

Because you have this:因为你有这个:

Blowfish(BlowfishAlgorithm algorithm);

It's not a default constructor.它不是默认构造函数。 The default constructor is one which takes no parameters.默认构造函数是一个不带参数的构造函数。 ie IE

Blowfish();

默认构造函数是没有参数的构造函数,或者如果它有参数,则所有参数都有默认值。

You declared the constructor blowfish as this:您将构造函数河豚声明为:

Blowfish(BlowfishAlgorithm algorithm);

So this line cannot exist (without further initialization later):所以这条线不能存在(以后没有进一步初始化):

Blowfish _blowfish;

since you passed no parameter.因为你没有传递参数。 It does not understand how to handle a parameter-less declaration of object "BlowFish" - you need to create another constructor for that.它不了解如何处理对象“BlowFish”的无参数声明——您需要为此创建另一个构造函数。

This is a typical human error.这是典型的人为错误。 Intelisense is asking for that for one reason, you are creating an object without parameters in the stack: Intelisense 要求这样做的原因之一是,您正在堆栈中创建一个没有参数的对象:

public:
Blowfish _blowfish;

That declaration requires a default constructor.该声明需要一个默认构造函数。 One option to correct this is to create the object with new.纠正此问题的一种选择是使用 new 创建对象。

Blowfish myBlowfish = new Blowfish(algorithm);

The second options is to create default constructor as requested.第二个选项是根据要求创建默认构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM