Как исправить ошибку типа при работе с stack.top () в библиотеке <stack>в c ++ - PullRequest
0 голосов
/ 26 сентября 2019

Я пытаюсь реализовать функцию в своем связанном списке, которая помещает значения одного списка в стек, а затем выталкивает эти значения в другой список.

Проблема в том, что когда я пытаюсьstd::cout << x, самый верхний элемент стека first, я получаю эту ошибку:

c: \ mingw \ lib \ gcc \ mingw32 \ 8.2.0 \ include \ c ++ \ ostream: 682: 5: ошибка: нет типа с именем 'type' в 'struct std :: enable_if'

#include <iostream>
#include <cstddef>
#include <string>
#include <stack>
#include <vector>

using Item = std::string;

class List {


public:

  class ListNode { //Changed this to public so I could access it. If this was in private how would I accomplish this?
  public:
    Item item;
    ListNode * next;
    ListNode(Item i, ListNode *n=nullptr) {
      item = i;
      next = n;
    }
  };

  ListNode * head;
  ListNode * tail; //Also in private, changed to public, so I could access it. This is the bottom boundry.

  class iterator {
    ListNode *node;
  public:
    iterator(ListNode *n = nullptr) {
      node = n;
    }
    Item& getItem() { return node->item; }
    void next() { node = node->next; }
    bool end() { return node==nullptr; }

    friend class List;
  };

public:
  List() {
    // list is empty
    head = nullptr;
    tail = nullptr;
  }

  List(const List &other)
  {
    iterator o = other.begin();
    while(!o.end())
      {
         append(o.getItem());   
         o.next();
      }
  }

  bool empty() {
    return head==nullptr;
  }

  // Only declared, here, implemented
  // in List.cpp
  void append(Item a);
  bool remove (Item &copy);

  void insertAfter(iterator, Item);
  void removeAfter(iterator, Item&);

  iterator begin() const {
    return iterator(head);
  }
};


void List::append(Item a) {
  ListNode *node = new ListNode(a);
  if ( head == nullptr ) {
    // empty list
    head = node;
    tail = node;
  } else {
    tail->next = node;
    tail = node;
  }
}

bool List::remove(Item &copy)
{
  if (!empty()) {
    copy = head->item;
    ListNode *tmp = head->next;
    delete head;
    head = tmp;
    if (head==nullptr)
      tail = nullptr;
    return true;
  }
  return false;
}

void List::insertAfter(iterator it, Item item)
{
  if (it.node == nullptr)
    {
      // insert at head
      ListNode *tmp = new ListNode(item,head);
      // if list is empty, set tail to new node
      if (tail==nullptr) {
    tail = tmp;
      }
      // set head to new node
      head = tmp;
    }
  else
    {
      ListNode *tmp = new ListNode(item,it.node->next); 
      it.node->next = tmp;
      // could be a new tail, if so update tail
      if (tail==it.node) {
    tail = tmp;
      }     
    }
}

void List::removeAfter(iterator it, Item& item)
{
  // emtpy list or at tail, just return
  if (it.node == tail) return;

  if (it.node == nullptr)
    {
      ListNode * tmp = head;
      head = head->next;
      delete tmp;
      if (head==nullptr)
    tail = nullptr;
    }
  else
    {
      ListNode *tmp = it.node->next;
      it.node->next = tmp->next;
      delete tmp;
      // could be that it.node is the new nullptr
      if (it.node->next == nullptr)
    tail = it.node;
    }
}

List reverse(const List &l)
{
    List::iterator iter1 = l.begin();
    std::stack<List::ListNode> first; 

    while(!(iter1.end())) {
        first.push(iter1.getItem());
        iter1.next();
    }

    List lthe3;
    int z = first.size();

    for (int i=0; i < z; ++i) {
        List::ListNode x = first.top();
        std::cout << x;
    }

  return l;
}

void printList(List &l) {
  List::iterator i = l.begin();
  while(!i.end()) {
    std::cout << i.getItem() << ", ";    
    i.next();
  }
  std::cout << std::endl;
}

int main()
{
  List l;
  l.append("one");
  l.append("two");
  l.append("three");
  l.append("four");

  std::cout << "List in order: ";
  printList(l);

  List l2 = reverse(l);
  std::cout << "List in reverse order: ";
  printList(l2);


  return 0;
}

Понятия не имею, что я делаю неправильно, но я действительно близок к завершению этой функции.

Могу ли я получить какую-то обратную связь?

...