飘逸的石头 2017-11-03 13:12 采纳率: 33.3%
浏览 7743
已采纳

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指针报错,一直不懂为什么?求大神指导 急急急

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-11-03 15:52
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?