Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no default constructor exists for class

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

like image 871
Abanoub Avatar asked Feb 12 '11 23:02

Abanoub


People also ask

Can a class have no default constructor?

Not having a default constructor is no problem, as long as you don't use one. Always specifying an argument at construction is ok, when there is no obvious default argument.

What happens if there is no default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Do you need to have a default constructor C++?

For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible. All data members of reference type and all const members must have a default member initializer.

Is default constructor is not defined then how the objects of the class will be created?

Default constructor must be defined, if parameterized constructor is defined and the object is to be created without arguments. Explanation: If the object is create without arguments and only parameterized constructors are used, compiler will give an error as there is no default constructor defined.


1 Answers

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.

like image 94
Jerry Coffin Avatar answered Sep 20 '22 11:09

Jerry Coffin