問題描述
#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.
這篇關于類不存在默認構造函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!