使用链栈时类型提示错误:无法从”Node<T> 转换为 int “?

这个代码是使用链栈进行数制转换的。代码如下:
头文件:

template<class T>
struct Node{
    T data;
    Node<T>* next;

};
template<class T>
class LinkStack{
public:
    LinkStack(){ top = NULL; }
    ~LinkStack();
    void Push(T x);
    T Pop();
    int convert(int z,int ss);
private:
    Node<T>*top;
    T *p;
};
template<class T>void LinkStack<T>::Push(T x){
    s = new Node<T>;
    s->data = x;
    s->next = top;
    top = s;
}
template<class T>T LinkStack<T>::Pop(){
    int x; 
    x = top->data;
    p = top;
    top = top->next;
    delete p;
    return x;
}
template<class T>int LinkStack<T>::convert(int z, int ss){
    int x = 0, count = 0;
    while (z != 0){
        x = z%ss;
        Push(x);
        z = z / ss;
        count = count + 1;
    }
    return count;

}

主函数:

#include<iostream>
#include"LinkStack.h"
using namespace std;
void main(){
    LinkStack<int> s;
    s.convert(1, 2);
    cout<<s.Pop();
} 

Pop方法里提示,

这个地方类型不能转换。但是前面的P定义成T类型了,为什么还会这样?请大家帮忙看看

阅读 2.5k
1 个回答

既然你要将top赋值给pp就应当用和top相同的类型啊,p也要改成Node<T> *

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进