繁体   English   中英

C ++调用两次导致访问冲突

[英]C++ call twice cause access violation

第一次尝试调用that-> getValue()看起来不错,但是仅下面一行(while循环的开头)在getValue方法内给我错误“访问冲突”。

if(first != 0){
  listElement *that = first;    

  cout << "add: " <<    that->getValue() << " | " << value  << endl; 

  while(that->getValue() < value) {..}
}

我可以在通话中的任何地方编辑值吗? get方法仅由“返回值”组成。

明显的解释是在这段代码中

while(that->getValue() < value) {..}

{..}内部,您正在执行that = that->next; 并设置that空指针。

您需要在while循环中添加针对that != NULL的测试,以防止到达列表末尾而没有找到符合搜索条件的项目。

while(that != NULL && that->getValue() < value)

如果您包括所有代码,那将会有所帮助,因为似乎代码的关键点在{..}块中!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM