初始化一个包含自身向量的结构体
我有一个菜单系统,我想从常量数据初始化。 MenuItem
可以包含 MenuItems
向量作为子菜单。但它只能在一定程度上发挥作用。以下是问题的要点:
#include <vector>
struct S { std::vector<S> v ; } ;
S s1 = { } ;
S s2 = { { } } ;
S s3 = { { { } } } ;
g++ -std=c++0x
(版本 4.4.5)处理 s1
和 s2
,但 s3
返回:(
prog.cpp:6:22: error: template argument 1 is invalid
请参阅 ideone)。我做错了什么吗?
I have a menu system that I want to initialise from constant data. A MenuItem
can contain, as a sub-menu, a vector of MenuItems
. But it only works up to a point. Here are the bare bones of the problem:
#include <vector>
struct S { std::vector<S> v ; } ;
S s1 = { } ;
S s2 = { { } } ;
S s3 = { { { } } } ;
g++ -std=c++0x
(version 4.4.5) copes with s1
and s2
, but s3
comes back with:
prog.cpp:6:22: error: template argument 1 is invalid
(see ideone). Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GMan 的评论是正确的:在代码中的
S::v
声明中,S
仍然不完整。类型必须完整才能用作 STL 容器中的值类型。 请参阅 Matt Austern 的文章“标准图书馆员:不完整类型的容器”。有关详细信息, 如果容器可以与不完整的类型一起使用,那么您的代码就可以了。例如,给出以下内容:
那么您的原始初始化应该可以正常工作:
这也可以:
GMan is correct in his comment: in the declaration of
S::v
in your code,S
is still incomplete. A type must be complete to be usable as the value type in an STL container. For more information see Matt Austern's article "The Standard Librarian: Containers of Incomplete Types."If you were to switch to a container that is usable with an incomplete type, then your code is fine. For example, given the following:
then your original initialization should work fine:
This would work too:
boost::optional 和 boost::recursive_wrapper 看起来对此很有用。
您添加的每个子菜单都需要 4 个大括号。当涉及构造函数调用时,不会发生大括号省略。例如
,老实说,使用构造函数看起来更具可读性,
现在它简化为
boost::optional and boost::recursive_wrapper look useful for this
You need 4 braces for every submenu you add. Brace elision does not happen when constructor calls are involved. For example
Honestly, using constructors look more readable
Now it simplifies to
您想要做的是 C++ 的
即将推出的当前功能,称为“初始化列表”,其中可以使用 = { } 初始化向量或列表。我不知道他们是否在TR1中推出了它。也许是TR2里的。
您使用的代码对我来说看起来不合适。如果要实现包含结构(树)的结构,请在节点内包含指向结构/节点的指针列表(如果不可实现,则仅包含空指针)。
大多数菜单结构本质上是一个基于列表的有序树(一个地方有 n 个节点,但其他地方可能有 m 个节点,等等)。 Robert Sedgewick 制作了一本教科书“C++ 算法”。
这里的问题是相互依赖,我想我已经通过类声明解决了这个问题。
执行 CTreeNode 类;在类 CTree {} 之前。
http://www.codeguru.com/forum/showthread.php?t=383253
我可能正在修改这段代码,而且它是不完整的,因为我已经很多年不需要编写树了,但我认为我已经涵盖了基础知识。我没有实现运算符[]。
what you are trying to do is an
upcomingcurrent feature of C++ called "initializer lists", where a vector or list can be initialized with = { }.I don't know if they have come out with it in TR1 or not. maybe it's in TR2.
the code you are using doesn't look proper to me. If you want to implement structures that contain structures (a tree), include a list of pointers to the structures/nodes (or just void pointers if that's not implementable) within the node.
most menu structures are essentially an ordered list-based tree (n nodes in one place, but could be m nodes elsewhere, etc). Robert Sedgewick makes a textbook "Algorithms in C++".
the problem is mutual dependency here, and I think I have it resolved with the class declaration.
do class CTreeNode; before class CTree {}.
http://www.codeguru.com/forum/showthread.php?t=383253
I am probably mangling this code, and it's incomplete, because I haven't had the need to write a tree in years, but I think I've covered the basics. I didn't implement operator[].