堆栈,链表模板错误

时间:2018-11-10 16:22:02

标签: c++

我的代码中有2个错误。

首先, listrec.cpp

中stackWritemirror函数出现2个错误
  1. 无效堆栈::推(const SE&):无法转换    ListNode *的参数1到const SE&。
  2. 无法从'='SE转换为ListNode *。`

这是我的代码。

不包含标题。

listrec.cpp

#include <iostream>
#include "listrec.h"
#include "stacklnk.cpp"

template<class LE>
void List<LE>::stackWriteMirror() const
{
    Stack<ListNode<LE*>> tempstack;
    ListNode<LE> *a;
    a = head;
    while (a != 0)
    {
        tempstack.push(a);
        a = a->next;
    }
    while (!tempstack.empty())
    {
        a = tempstack.pop();
        std::cout << a->element;

        a = a->next;
    }
}  

stacklnk.cpp

#include <assert.h>
#include "stacklnk.h"
template < class SE >
void Stack<SE>::push(const SE &newElement)
{

}
template < class SE >
SE Stack<SE>::pop()
{

}

需要帮助纠正这些错误。

1 个答案:

答案 0 :(得分:0)

第一个错误是因为,显然,您正在将ListNode类型的变量推入tempstack中,而不是ListNode中。

第二个错误来自“第一个错误”中的类型问题。如果将变量a的类型从ListNode<LE>*更改为ListNode<LE*>,它将被固定。

我认为您需要研究更多有关在c ++ c中定义变量的信息。

#include <iostream>

template<class LE>
void List<LE>::stackWriteMirror() const
{
    Stack<ListNode<LE*>> tempstack;
    ListNode<LE*> a; // first error edited
    a = head;
    while (a != 0)
    {
        tempstack.push(a);
        a = a->next;
    }
    while (!tempstack.empty())
    {
        a = tempstack.pop(); // Second error comes from this line, I expect?
        std::cout << a->element;

        a = a->next;
    }
}