C++的模板类中使用链表,却在一些环节报错说一些指针为空

查看:139
本文介绍了C++的模板类中使用链表,却在一些环节报错说一些指针为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

C++的模板类中使用链表,却在一些环节报错说一些指针为空,但是这些地方都是限制过只有非空指针才能进的循环,没有理由会报错,求解。

#include<iostream>

using namespace std;

//链表节点
template<typename T>
struct List {
    T num;
    List* next;
};

//数组类
template<typename T>
class SString {
public:
    template<typename T>
    friend ostream& operator<<(ostream& out, const SString<T>& string);
    SString() {
        head = NULL;
    }
    SString(const SString& string) {
        List<T> *t;
        for (t = string.head; t != NULL; t = t->next) {
            *this + t->num;
        }
    }
    ~SString() {
        List<T> *t1, *t2;
        t1 = head;
        while (t1 != NULL) {
            t2 = t1->next;
            delete t1;
            t1 = t2;
        }
    }
    SString<T>& operator=(const SString<T>& string);
    SString<T>& operator+(T x);
    SString<T>& operator-(T x);
    SString<T> operator+(const SString<T>& string)const;
    SString<T> operator-(const SString<T>& string)const;
    SString<T> operator*(const SString<T>& string)const;
private:
    List<T> *head;
};

template<typename T>
SString<T>& SString<T>::operator+(T x) {
    List<T> *t1, *t2, *t3;
    t2 = new List<T>();
    t2->num = x;
    t2->next = NULL;
    if (head == NULL) {
        head = t2;
    }else{
        for (t1 = head; t1 != NULL; t1 = t1->next) {
            t3 = t1;
        }
     t3->next = t2;
    }
    return *this;
}

template<typename T>
SString<T>& SString<T>::operator-(T x) {
    List<T> *t1, *t2;
    for (t1 = head; t1 != NULL; t1 = t1->next) {
        if (t1->num == x) {
            t2->next = t1->next;
            delete t1;
            t1 = t2->next;
        }
        t2 = t1;
    }
    return *this;
}

template<typename T>
SString<T> SString<T>::operator+(const SString<T>& string) const
{
    List<T> *t;
    SString<T> s(*this);
    for (t = string.head; t != NULL; t = t->next) {
        cout << t->num;
        s.operator+(t->num);
    }
    return s;
}

template<typename T>
SString<T> SString<T>::operator-(const SString<T> & string) const
{
    SString<T> s(*this);
    List<T> *t;
    for (t = string.head; t != NULL; t = t->next) {
        s - t->num;
    }
    return s;
}

template<typename T>
SString<T> SString<T>::operator*(const SString<T> & string) const
{
    SString<T> s;
    s = *this - string;
    s = *this - s;
    return s;
}

template<typename T>
ostream& operator<<(ostream& out, const SString<T>& string)
{
    out << "{";
    List<T> *t;
    for (t = string.head; t != NULL; t = t->next) {
        out << t->num;
        if (t->next != NULL) {
            out << ",";
        }
    }
    out << "}";
    return out;
}

template<typename T>
SString<T>& SString<T>::operator=(const SString<T>& string) {
    if (this == &string) {
        return *this;
    }
    List<T> *t;
    for (t = string.head; t != NULL; t = t->next) {
        *this + t->num;
        cout << t->num;
    }
    return *this;
}

int main() {
    SString<int> s1, s2, s3, s4, s5;
    for (int i = 0; i < 3; i++) {
        int num;
        cin >> num;
        s1 = s1 + num;
    }
    cout << s1 << endl;
    for (int i = 0; i < 3; i++) {
        int num;
        cin >> num;
        s2 = s2 + num;
    }
    cout << s2 << endl;
    cout << "ss";
    (s1 + s2);
    cout << "ss";

    //cout << s3 << endl;
    //s4 = s1 - s2;
    //cout << s4 << endl;
    //s5 = s1*s2;
    //cout << s5 << endl;
    return 0;
}

解决方案

代码的其中一段确实在gcc中编译不过,类型名字换一下就行了,然后就是运行没有问题,希望你把问题补充的再具体些。。比如出现错误的截图

template<typename X>
    friend ostream& operator<<(ostream& out, const SString<X>& string);

还有一个值得吐槽的地方,你的+运算符太迷了,改为+=或者像+(const SString&)一样不要返回引用。。

这篇关于C++的模板类中使用链表,却在一些环节报错说一些指针为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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