列表迭代器访问冲突

发布于 2024-11-04 02:19:57 字数 1499 浏览 4 评论 0原文

我有一个指向 Main 类中的 SomeClass 对象的列表。在 Main 中,我可以使用 list::begin()list::end() 迭代 list >。

当我从 SomeClass 的实例执行相同操作时(请记住 listMain 类的公共成员),我得到了以下异常:

0xC0000005:读取位置 0xbaadf0ad 时访问冲突。

我对 C++ 编码相当陌生,有 Python 背景,所以如果有些内容没有意义,我深表歉意。

以下是这种情况的示例:

编辑: Main 类代码实际上不在构造函数内,而是在名为 InitialiseObjects() 的方法中。

编辑:异常来自 list::being() 方法中的行 return (iterator(_Nextnode(_Myhead), this));

#include <list>
using namespace std;

class Main
{
public:
    Main::InitialiseObjects()
    {
        // Execution starts here
        SomeClass someClass = new SomClass(this);
        someClasses.push_back(someClass);

        // This works fine
        for (list<SomeClass*>::iterator it = someClasses.begin(); it != someClasses.end(); it++)
            ...

        someClass->AFunction();
    }
    list<SomeClass*> someClasses;
}

class SomeClass
{
public:
    SomeClass::SomeClass(Main *main) : main(main) {}
    void SomeClass::AFunction()
    {
        // This will not work throwing the aformentioned error
        for (list<SomeClass*>::iterator it = main->someClasses.begin(); it != main->someClasses.end(); it++)
            ...
    }
private:
    Main *main;
}

I have a list of pointers to SomeClass objects in a Main class. Whilst in Main I can iterate over the list using list::begin() and list::end().

When I do the same from an instance of SomeClass (bearing in mind that the list is a public member of the Main class) I get the following exception:

0xC0000005: Access violation reading location 0xbaadf0ad.

I am fairly new to coding in C++ coming from a Python background, so I apologise if something doesn't make sense.

Here is an example of the situation:

EDIT: The Main class codes is not actually inside the constructor, its in a method called InitialiseObjects().

EDIT: The exceptions comes from within the list::being() method with the line return (iterator(_Nextnode(_Myhead), this));

#include <list>
using namespace std;

class Main
{
public:
    Main::InitialiseObjects()
    {
        // Execution starts here
        SomeClass someClass = new SomClass(this);
        someClasses.push_back(someClass);

        // This works fine
        for (list<SomeClass*>::iterator it = someClasses.begin(); it != someClasses.end(); it++)
            ...

        someClass->AFunction();
    }
    list<SomeClass*> someClasses;
}

class SomeClass
{
public:
    SomeClass::SomeClass(Main *main) : main(main) {}
    void SomeClass::AFunction()
    {
        // This will not work throwing the aformentioned error
        for (list<SomeClass*>::iterator it = main->someClasses.begin(); it != main->someClasses.end(); it++)
            ...
    }
private:
    Main *main;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-11-11 02:19:57
0xC0000005: Access violation reading location 0xbaadf0ad.

我认为那是0xbaadf00d,直到您覆盖其中的某些内容,这表明未初始化的堆内存。那么,也许您正在取消引用不存在的 Main* ?例如,如果 main 尚未初始化,main->someClasses.begin() 可能会抛出异常。
事实上,这就是您的代码中的问题。 someClass->AFunction(); 当您位于 Main 构造函数内时调用该函数。因此,this 尚未完全有效,因此您无法在 AFunction 内使用它。

0xC0000005: Access violation reading location 0xbaadf0ad.

I think that was 0xbaadf00d until you overwrote something in it, which indicates uninitialized heap memory. So, maybe you're dereferencing a non-existant Main*? E.g. main->someClasses.begin() may throw your exception, if main wasn't initialized yet.
And indeed, this is the problem in your code. someClass->AFunction(); calls the function while you're inside the Main-constructor. As such, this will not be completely valid yet, so you can't use it inside the AFunction.

冷情妓 2024-11-11 02:19:57

只要您留在 Main 构造函数中,this 实例就尚未完全初始化

尝试从其他地方访问实例会导致未定义的行为。由于您是从 SomeClass 中访问这个未完成的实例,因此会发生这种情况。

As long as you remain inside the Main constructor, the this instance is not yet fully initialised!

Trying to access the instance from elsewhere leads to undefined behaviour. This is happening here since you are accessing this unfinished instance from within SomeClass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文