std::initializer_list
is a class template that holds a sequence of initializers, which has a very minimal interface since its only purpose is to be used to initialize another object with the sequence of values it contains. Unlike std::vector
or std::deque
, std::initializer_list
is not intended to be used as a general-purpose container.
The function LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>&)
is called when someone attempts to list-initialize a LinkedQueue<T>
, like this:
LinkedQueue<int> q {1, 2, 3};
The language automatically constructs a std::initializer_list<int>
object which contains the values 1, 2, 3, in that order. This object is passed to the constructor. After the constructor returns, the user expects that the newly constructed LinkedQueue<int>
object will contain the values 1, 2, 3, in that order.
You need to write the constructor such that this will be true. To do so, you can iterate over the std::initializer_list<T>
object using a range-based for loop and add the values in order to the list. Something like this might work, assuming you have a push_back
function and a working default constructor:
template<class T>
LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il): LinkedQueue() {
for (const T& val : il) {
push_back(val);
}
}
This delegates to the default constructor to set up the class invariants, then inserts the given initializers in order.