Я новичок в C ++.Я начинаю создавать список контейнеров, используя шаблон класса, но компилятор VS выдает мне ошибку, если я создаю экземпляр шаблона класса List в main следующим образом: List l (5);Если я удаляю эту строку в main (), код компилируется нормально.Или, если я определяю класс Node вне класса List, у меня нет этой ошибки.И компилятор выдает ошибку в этой строке кода:
node_ptr head = (alloc.allocate(s));
Пожалуйста, помогите.Большое спасибо!
#include "pch.h"
#include <iostream>
#include <memory>
using namespace std;
template<class T> class Node; //forward declaration
template< class T, typename Allocator = std::allocator<Node<T>>>
class List
{
using data_ptr = T *;
using data_type = T;
class Node {
public:
T value;
Node* next;
Node() : value(data_type()), next(0) {}
};
using node = Node;
using node_ptr = Node*;
public:
List() : length(0), head(NULL), alloc(std::allocator<int>()) {}
explicit List(size_t s) : length(s), head(NULL), alloc(std::allocator<Node>())
{
node_ptr head = (alloc.allocate(s));
}
~List() {};
//private:
node_ptr head;
size_t length;
Allocator alloc;
};
int main()
{
List<int> l(5); //The compile error is gone if this line is removed
system("pause");
return 0;
}