English 中文(简体)
C++ 与目标瞄准者相遇
原标题:C++ trouble with pointers to objects

我有一个带有指向对象的向量的类。我已经在这个向量中引入了一些元素,并且在我的主文件中,我已经成功地打印它们并添加了其他元素,没有出现任何问题。现在我正在尝试从向量中删除一个元素并检查它是否不为空,但它不起作用。

I m 填充时用舱面测试:

Other *a = new Other(1,1);
Other *b = new Other(2,2);
Other *c = new Other(3,3);

v->push_back(a);
v->push_back(b);
v->push_back(c);

在我的主要档案中,我有:

Test t;
(...)

Other *pointer = t.vect->at(0);

delete t.vect->at(0);
t.vect->erase(t.vect->begin());

if (pointer == NULL) { cout << "Nothing here.."; } // Never enters here..
最佳回答

删除点人指出的记忆,并没有给民族解放军确定点。

问题回答

删除一个点子必须等于零,它只是释放了在那里分配的记忆。 使用<代码>pointer在删除后未作界定,因为记忆可用于其他用途。 从技术上讲,C++的幽灵故意使其无法执行,不管他们是否想要零一个被删除的点,但我知道实际上没有这样做。

你定点等于一点,从来不会再次触及这个问题,当然,它赢得了失败。 你当时对目标做了一些事情,这是不相干的。

使用简单点的人,没有安全的途径来确定其曾经指出的物体是否已被删除。 您似乎想做的最简单办法是将其留给集装箱:如果你对这个物体重新感兴趣,则寻找病媒的点人,看看该物体是否仍在那里(并且不会删除该物体,而不会从病媒中删除相应的成分,或者你再次有同样的问题)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签