Mostafa Talebi Mostafa Talebi - 1 month ago 12
C++ Question

Cannote create an object, constructor error (for object initialization)

I have created an Stack extension for

std::stack
.

I have created a template class whose code is here:

template<class _StackType>
class Stack
{
std::stack<_StackType> data_container;
mutable std::mutex M;
public :

Stack(const Stack& other)
{
std::lock_guard<std::mutex> lock(other.M);
this->data_container = other.data_container;
}


But when I initialize it :

Stack<int> myStack;


It throws the following error:

error: no matching function for call to `‘Stack<int>::Stack()’`


It seems there is some problems with operator. I did try to create an operator overloading but failed at my attempts.

What is the cause of error?

Answer

You didn't specify a default constructor for Stack, so the variable myStack can't be created.

Normally, the default constructor is implicit, but in your case, because you specified the copy constructor, it is deleted.

Either implement it yourself, or default it explicitly:

Stack() = default; // Default implementation of default constructor