没有默认的构造函数 [英] no default constructor exists for class

查看:97
本文介绍了没有默认的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include Includes.h 


枚举BlowfishAlgorithm
{
ECB,
CBC,
CFB64,
OFB64,
};

类河豚
{
public:
struct bf_key_st
{
unsigned long P [18];
无符号长S [1024];
};
Blowfish(BlowfishAlgorithm算法);
void Dispose();
void SetKey(unsigned char data []);
unsigned char Encrypt(unsigned char buffer []);
unsigned char Decrypt(unsigned char buffer []);
char EncryptIV();
char DecryptIV();
私人:
BlowfishAlgorithm _algorithm;
unsigned char _encryptIv [200];
unsigned char _decryptIv [200];
int _encryptNum;
int _decryptNum;
};

类GameCryptography
{
public:
河豚_河豚;
GameCryptography(unsigned char key []);
void Decrypt(unsigned char packet []);
void Encrypt(unsigned char packet []);
河豚河豚;
void SetKey(unsigned char k []);
void SetIvs(unsigned char i1 [],unsigned char i2 []);
};




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

错误:IntelliSense:类 Blowfish没有默认的构造函数?

解决方案

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



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

  Blowfish(BlowfishAlgorithm algorithm = CBC); 

...因此可以调用ctor,而无需(明确地)指定算法(在这种情况下,



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

 类GameCryptography {
河豚河豚_;
public:
GameCryptography():blowfish_(ECB){}
// ...
};

在C ++ 11(或更高版本)中,您还有一个可用的选择。您可以定义一个带参数的构造函数,但是告诉编译器生成如果不定义一个则具有的构造函数:

  class GameCryptography {
public:

//定义接受参数的ctor
GameCryptography(BlofishAlgorithm);

//告诉编译器执行未定义ctor的操作:
GameCryptography()= default;
};作为最后的说明,我认为值得一提的是,欧洲央行,CBC,CFB等是操作模式,而不是加密算法本身。称它们为算法不会打扰编译器,但是 可能会给其他阅读代码的人造成不合理的问题。


#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 -- 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.

这篇关于没有默认的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆