C++的模板类中使用链表,却在一些环节报错说一些指针为空
迷茫
迷茫 2017-04-17 13:45:08
0
2
565

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;
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
黄舟

One section of the code does not compile in gcc. Just change the type name and it runs without problems. I hope you can make the question more specific. . For example, a screenshot of an error

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

There is another point worth complaining about. Your + operator is too confusing. Change it to += or do not return a reference like +(const SString&). .

Ty80
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, *t3;
    t2 = new List<T>();
    t2->num = x;
    t2->next = NULL;
    if (head == NULL) {
        head = t2;
    }
    else{
        for (t1 = head; t1->next != NULL; t1 = t1->next)
            ;
        t3 = t1;
        t3->next = t2;
    }
    return *this;
}

If you change it to this, it will compile in VS. I don’t know the reason. It prompts that t3 may not be initialized, but the logic loop body will definitely execute

But I still encountered a problem after changing it

    SString() {
        head = NULL;
    }
    SString(const SString& string) {
        List<T> *t;
        for (t = string.head; t != NULL; t = t->next) {
            *this + t->num;
        }
    }

Your copy constructor does not initialize head, but

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;
}

When s is returned here, the copy constructor will be called. Since head is not initialized,

*this + t->num;

The wrong head address is passed in this sentence

But I don’t know why the head is initialized to NULL when calling the copy constructor except when returning a value. Novices are welcome to answer.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template