-2

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?

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • There's no operator involved?? You simply missed to specify a default constructor. In case a copy constructor is defined, the compiler won't generate one automatically. – πάντα ῥεῖ Oct 18 '16 at 18:54

2 Answers2

3

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
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
1

Your Stack class is missing a default constructor.

Donghui Zhang
  • 1,133
  • 6
  • 8