English 中文(简体)
我为什么在呼吁提升前线时出现错误:从速从名单上删除?
原标题:Why do I get an error when calling front() on boost::ptr_list?
  • 时间:2011-04-11 10:16:50
  •  标签:
  • c++

我有一个虚拟班子<代码>basic_action。 <代码>sippeers 继承类别basic_action。 储存<代码>sippeers 班级 使用<代码>boost:ptr_list/code>。 这里的例子有:

boost::ptr_list<basic_action> ActionsList;
sippeers spclass;
ActionsList.push_back(&spclass);
basic_action *sp = ActionsList.front();

Here I create an instance of prt_list with pointers to instances of my basic_action classes. Next I make new instance of my sippeers class. Next I insert pointer to sippeers class into ptr_list.

最后一点是失败的。

Cannot convert from basic_action to basic_action * .

但是,在座的有<编码>basic_action *内,而不是basic_action!

最佳回答

boost::ptr_list::front() returns a reference to the templated type, not a pointer.

因此,在这种情况下,它退回了<代码>basic_action&。

here,ptr_sequence_adapter

因此,你的法典应改为:

boost::ptr_list<basic_action> ActionsList;
sippeers spclass;
ActionsList.push_back(&spclass);
basic_action &sp = ActionsList.front();
问题回答

ptr_list::front() returns a reference to the first object in the list. If basic_action was a concrete type you could both of the following.

// 1
basic_action& sp = ActionsList.front();

// 2
basic_action sp = ActionsList.front();

第1号将提及名单上的第一个物体。 换言之,你通过间谍进行的任何改动也将改变名单上的第一个目标。

第2号将给这个新物体注入新的基本用途。 对清单的任何改动都不会影响清单上的第一个项目。

如果基本的——行动是一种抽象的类别选择,那么,由于你无法即时提出抽象的阶级目标,你就不再能够使用。

并且,你也把ack放在推进器箱子里。 当小号清单偏离范围并试图删除其所含的所有物体时,会发生相反的情况。 相反地做的是:

boost::ptr_list<basic_action> ActionsList;
ActionsList.push_back(new sippeers);
basic_action& sp = ActionsList.front();




相关问题
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?

热门标签