C ++调用两次导致访问冲突(C++ call twice cause access violation)

编程入门 行业动态 更新时间:2024-10-10 03:28:55
C ++调用两次导致访问冲突(C++ call twice cause access violation)

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

if(first != 0){ listElement *that = first; cout << "add: " << that->getValue() << " | " << value << endl; while(that->getValue() < value) {..} }

我可以在通话过程中编辑值吗? get方法只包含“返回值”....

the first try to call that->getValue() looks good, but just the line below (the head of while loop) give me the error "access violation" inside the getValue method.

if(first != 0){ listElement *that = first; cout << "add: " << that->getValue() << " | " << value << endl; while(that->getValue() < value) {..} }

Do i edit the the value during the call anywhere? The get method consists just of "return value"....

最满意答案

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

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

在{..}你正在做that = that->next; 并将其设置为空指针。

你需要在while循环中添加一个测试that != NULL ,以防止到达列表的末尾,而不会找到符合搜索条件的项目。

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

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

The obvious explanation is that in this code

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

inside the {..} you are doing that = that->next; and setting that to the null pointer.

You need to add a test for that != NULL in your while loop to protect against getting to the end of the list without finding an item that meets your search criterion.

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

It would have helped if you had included all the code because it seems that the key bit of code is in the {..} block!

更多推荐

本文发布于:2023-08-04 01:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   冲突   call   access   violation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!