no default constructor exists for class

Abanoub picture Abanoub · Feb 13, 2011 · Viewed 137.8k times · Source
#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" ???!

Answer

Jerry Coffin picture Jerry Coffin · Feb 13, 2011

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.