C++ 类不存在默认构造函数

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

no default constructor exists for class

c++

提问by Abanoub

#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" ???!

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

回答by Jerry Coffin

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 dodefine a constructor, (even if it does take one or more arguments) the compiler will notsynthesize 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. 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:

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

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. Calling them algorithms won't bother the compiler, but isunreasonably likely to cause a problem for others reading the code.

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

回答by Dave

Because you have this:

因为你有这个:

Blowfish(BlowfishAlgorithm algorithm);

It's not a defaultconstructor. The default constructor is one which takes no parameters. i.e.

它不是默认构造函数。默认构造函数是一个不带参数的构造函数。IE

Blowfish();

回答by Mārti?? Briedis

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

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

回答by RageD

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”的无参数声明——您需要为此创建另一个构造函数。