类不存在默认构造函数

发布于 2024-10-17 07:02:22 字数 1082 浏览 8 评论 0原文

#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[])
{
}

错误:IntelliSense:类“Blowfish”不存在默认构造函数???!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丢了幸福的猪 2024-10-24 07:02:23

因为你有这个:

Blowfish(BlowfishAlgorithm algorithm);

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

Blowfish();

Because you have this:

Blowfish(BlowfishAlgorithm algorithm);

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

Blowfish();
怎会甘心 2024-10-24 07:02:23

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

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

断舍离 2024-10-24 07:02:23

您将构造函数 Blowfish 声明为:

Blowfish(BlowfishAlgorithm algorithm);

因此这一行不能存在(稍后无需进一步初始化):

Blowfish _blowfish;

因为您没有传递任何参数。它不理解如何处理对象 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.

Note: If you define user-provided constructor(s), the default constructor will disappear.

伴我心暖 2024-10-24 07:02:23

这是典型的人为错误。 Intelisense 要求这样做的原因之一是,您正在堆栈中创建一个没有参数的对象:

public:
Blowfish _blowfish;

该声明需要一个默认构造函数。纠正此问题的一种选择是使用 new 创建对象。

Blowfish myBlowfish = new Blowfish(algorithm);

第二个选项是根据要求创建默认构造函数。

This is a typical human error. Intelisense is asking for that for one reason, you are creating an object without parameters in the stack:

public:
Blowfish _blowfish;

That declaration requires a default constructor. One option to correct this is to create the object with new.

Blowfish myBlowfish = new Blowfish(algorithm);

The second options is to create default constructor as requested.

过期情话 2024-10-24 07:02:22

如果您定义一个没有任何构造函数的类,编译器将为您合成一个构造函数(这将是一个默认构造函数 - 即不需要任何参数的构造函数)。但是,如果您确实定义了一个构造函数(即使它确实需要一个或多个参数),编译器将不会为您合成一个构造函数——此时,您已经负责构造该类的对象,因此编译器“后退一步”,可以这么说,并将这项工作留给您。

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

Blowfish(BlowfishAlgorithm algorithm = CBC);

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

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

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

在 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;
};

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

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);

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

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

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:

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 is unreasonably likely to cause a problem for others reading the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文