No Default Constructor Exists for Class X (Inheritance) C++

no default constructor exists for class x (inheritance) C++

When you declare a non-default constructor for a class, the compiler does not generate a default one anymore. So you have to provide your own.

PlayerStates needs to call a default constructor of its base class Player in its own default constructor. So you either need to provide Player with a default constructor, or call it's non-default constructor from PlayerStates' default constructor initialization list.

This implicitly calls Player::Player().

PlayerStates::PlayerStates() : textureAmount( 3 ) {}

This calls the single argument constructor:

PlayerStates::PlayerStates() : Player(someComPtr), textureAmount( 3 ) {}

No default constructor exists for class class

If A didn't had its default constructor deleted (by defining any constructor), this code would work. Either you have to call A constructor in initialization list of D.

In case of virtual inheritance, instances of B and C are subobjects of class D, but they don't contain subobject of class A. Instead a single instance of A is subobject of D.

This way there is only one A in D, which solves so called cursed diamond at cost of B,C and D no longer having a standard memory layout ("memcpy-able") even if it didn't had any virtual members and D should have access to constructor of A to be able initialize it.

No default constructor exists for class error

The problem is here:

Stuff(Thing thing) {
this->thing = thing;
}

By the time you enter the constructor's body, the compiler will have already initialized your object's data members. But it can't initialize thing because it does not have a default constructor.

The solution is to tell the compiler how to initialize it by using an initlizer list.

Stuff(Thing thing) : thing(thing) {
// Nothing left to do.
}

This is less typing, cleaner code and more efficient. (More efficient, because if the variable is going to be initialized anyway, then why initialize it with an unwanted value first just to assign another one as quickly as you can? Of course, since your current code doesn't even compile, “more efficient” is a somewhat dubious statement, here.)

no default constructor exists for class

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.

C++: No default constructor exists for parent class

You got it there, Tree does not have a default constructor. As a result of that, you cannot default-construct a BST, because the Tree that is inside it will not know how to construct itself.

You have to member-initialize it.

BST::BST() : Tree(1) {}

Are you sure you don't want to pass that value to the constructor of BST though?

BST::BST(int w) : Tree(w) {}

Initialize base class with no default constructor in constructor of derived class

Because you don't have a default constructor the compiler complains that it can't create an object for primary, you should either add a parameter to the secondary constructor / give it a default value:

class secondary : public primary
{
public:
secondary(int x);
virtual ~secondary();
protected:
private:
};

And then call the base class constructor:

secondary::secondary(int x) : primary(x)
{
//ctor
}

Or:

secondary::secondary() : primary(5)
{
//ctor
}

Or just add a default constructor for primary:

class primary
{
public:
primary(int x);
primary() : primary_x(0) {}
virtual ~primary();
protected:
private:
int primary_x;
};

c++ inheritance with default constructor

manager basically contains an employ (though in the form of inheritance). So to be able to construct a manager, you need to be able to construct the contained employ. So you have to either provide a default constructor for it, or construct it explicitly by calling an existing constructor.

To construct it explicitly, you have to do something like this:

manager(int x): employ(#some istream here#)
{}

Base class don't have default constructor when derived class constructor initialization lists

A default constructor won't be generated if at least a user-defined constructor exists. And in your case, you have one.

C++ inherit class shows no default constructor

Your base class has no default constructor, which is what gets implicitly called in your current derived class constructor. You need to explicitly call the base's constructor:

IntroState::IntroState(ImageService& imageService) : State(imageService)
{

}


Related Topics



Leave a reply



Submit