数据结构 - 使用链栈时类型提示错误

查看:145
本文介绍了数据结构 - 使用链栈时类型提示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

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类型了,为什么还会这样?

解决方案

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

这篇关于数据结构 - 使用链栈时类型提示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆