c++链表 引发了异常: 读取访问权限冲突。 temp 是 0xCDCDCDCD。

 template<class T>
struct ChainNode {

    T element;
    ChainNode<T> *next;

    ChainNode();

    ChainNode(const T &element) {
        this->element = element;
    };

    ChainNode(const T &element, ChainNode *next) {
        this->element = element;
        this->next = next;
    };
};
template<class T>
ChainNode<T>::ChainNode() {
    next = NULL;
};

template<class T>
class Linklist {
public:

    Linklist();

    void addAtLast(T &);

    void addAtFist(T &);

    void deleteTarget(T &);

    int searchTarget(T &);

    void output();

    void merge(Linklist<T> *);

    void reverse(Linklist<T> *);

    ~Linklist();

private:
    ChainNode<T> *first;
};

template <class T>
Linklist<T>::Linklist() {
    first = NULL;
};
template <class T>
Linklist<T>::~Linklist() {
};
template<class T>
void Linklist<T>::addAtLast(T &element) {
    ChainNode<T> *temp = first;
        if ( temp != NULL) {
            while (temp->next != NULL) {
               temp = temp->next;
               std::cout << "yici";
            }
            temp->next = new ChainNode<T>(element);

        }
        else {
            first = new ChainNode<T>(element);

        }

};
int main() {

    std::cout << "Input1" << std::endl;
    int n;
    Linklist<int> *list = new Linklist<int>();
    int a = 5;
    int b = 6;
    list->addAtLast(a);
    list->addAtLast(b);
    }




在调用 addAtlast方式时 while 判断条件时 temp指针报错,一直不懂为什么?求大神指导 急急急

0xCDCDCDCD
看都不要看就知道这个指针所在的结构体没有初始化。

 first = new ChainNode<T>(element);
后面加上 first->next = NULL;