初始化包含自身向量的结构

| 我有一个要从常量数据初始化的菜单系统。
MenuItem
可以包含a1ѭ的向量作为子菜单。但这只能起到一定作用。这是问题的基本内容:
#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)。难道我做错了什么?     
已邀请:
GMan的评论正确无误:在代码中的
S::v
声明中,
S
仍不完整。类型必须完整才能用作STL容器中的值类型。有关更多信息,请参见Matt Austern的文章“标准库管理员:不完整类型的容器。” 如果您要切换到可用于不完整类型的容器,那么您的代码就可以了。例如,给出以下内容:
#include <initializer_list>

template <typename T>
struct Container
{
    Container() { }
    Container(std::initializer_list<T>) { }
};

struct S { Container<S> v; };
那么您的原始初始化应该可以正常工作:
S s3 = { { { } } } ;
这也可以工作:
S s4 = { { { { { { { { { { { { { { { { /*zomg*/ } } } } } } } } } } } } } } } };
    
boost :: optional和boost :: recursive_wrapper对此很有用
struct S { // one brace
    boost::optional< // another brace
      boost::recursive_wrapper< // another brace
        std::vector< // another brace
          S
        >
      >
    > v; 
};
每个添加的子菜单需要4个花括号。涉及构造函数调用时,不会发生括号删除。例如
S m{{{{ 
  {{{{ }}}}, 
  {{{{ 
    {{{{ }}}}, 
    {{{{ }}}} 
  }}}} 
}}}}; 
老实说,使用构造函数看起来更具可读性
struct S {
    // this one is not really required by C++0x, but some GCC versions
    // require it.
    S(char const *s)
    :v(s) { } 

    S(std::string const& s)
    :v(s) { }

    S(std::initialize_list<S> s)
    :v(std::vector<S>(s)) { } 

    boost::variant<
      std::string,
      boost::recursive_wrapper<
        std::vector<
          S
        >
      >
    > v; 
};
现在简化为
S s{ 
  \"S1\", 
  {
    \"SS1\",
    \"SS2\",
    { \"SSS1\", \"SSS2\" }
  }
};
    
您正在尝试做的是C ++即将出现的当前功能,称为“初始化列表”,其中可以使用= {}初始化向量或列表。 我不知道他们是否在TR1中推出了它。也许在TR2中。
#include <vector>
#include <list>
#include <iostream>
using namespace std;
int main(void) {
    vector<int> vi = {1, 2, 3, 4, 5};
    list<int> li = {5, 4, 3, 2, 1, 0};
    cout<<\"vector size=\"<<vi.size()<<\", list size=\"<<li.size()<<endl;
    return 0;
}
您使用的代码对我来说不合适。如果要实现包含结构(树)的结构,请在节点内包括指向结构/节点的指针列表(如果无法实现,则仅包含void指针)。 大多数菜单结构本质上是一个基于列表的有序树(一个位置有n个节点,但其他位置可能有m个节点,等等)。 Robert Sedgewick编写了一本教科书“ C ++算法”。
#include <vector>
#include <iterator>
#include <string>
void * pRoot = NULL; //pointer to CTree
class CTreenode;
class CTree;
class CTree {
    public:
        vector<class CTreeNode> lctnNodeList; //list does not have at() or operator[]
        vector<class CTreeNode>::iterator lctni;
    public:
        CTree() {}
        ~CTree() {
            for (lctni=lctnNodeList.begin(); lctni != lctnNodeList.end(); nctni++) {
                if (NULL==lctni->getChildPtr()) {
                    //do nothing, we have already done all we can
                } else {
                    delete (CTree *)lctnNodeList.pChild;
                }
                //do action here
            }
        }
        void addToList(string& data, CTree * p) { 
            CTreeNode ctn(data, p);
            lctnNodeList.push_back(d); 
        }
        void eraseAt(size_t index) { 
            vector<class CTreeNode>::iterator i = lctnNodeList.begin();
            vector<class CTreeNode>::iterator i2 = lctnNodeList.begin();
            i2++;
            size_t x;
            for (x=0; x <= index; x++,i++,i2++) {
                if (index == x) {
                    lctnNodeList.erase(i,i2);
                    break;
                }
            }
        }
        void at(size_t index, string& returndata, CTree * &p) { 
            vector<class CTreeNode>::iterator i = lctnNodeList.begin();
            size_t x;
            for (x=0; x <= index; i++,x++) {
                if (x==index) {
                     i->getData(returndata, p);
                     break;
                }
            }
        }
        const CTreeNode& operator[](const size_t idx) {
            if (idx < lctnNodeList(size)) {
                return lctnNodeList.at(idx);
            } else {
                //throw an error
            }
        }
        const size() {
            return lctnNodeList.size();
        }
        //this can be applied to the root of the tree (pRoot).
        doActionToThisSubtree(void * root) {
            CTree * pct = (CTree *)root;
            for (pct->lctni=pct->lctnNodeList.begin(); pct->lctni != pct->lctnNodeList.end(); pct->nctni++) {
                //this is a depth-first traversal.
                if (NULL==pct->lctni->getChildPtr()) {
                    //do nothing, we have already done all we can
                    //we do this if statement to prevent infinite recursion
                } else {
                    //at this point, recursively entering child domain
                    doActionToThisSubtree(pct->lctni->getChildPtr());
                    //at thisd point, exiting child domain
                }
                //do Action on CTreeNode node pct->lctni-> here.
            }
        }
};
class CTreeNode {
    public:
        CTree * pChild; //CTree *, may have to replace with void *
        string sData;
    public:
        CTreeNode() : pChild(NULL) {}
        CTreeNode(string& data, pchild) : pChild(pchild) {
            sData = data;
        }
        ~CTreeNode() { 
             if (NULL!=pChild) { 
                 delete pChild;//delete (CTree *)pChild; 
                 pChild = NULL; 
             }
        void getChild(CTreeNode& child) { 
            child = *pChild;//child = *((CTree *)pChild); 
        }
        bool addChild(string& s) { 
            if (NULL==pChild) {
                return false;
            } else {
                pChild = new CTree;
            }
            return true;
        }
        void * getChildPtr() { return pChild; }
        void getData(string& data, CTree * &p) { //not sure about how to put the & in there on CTree
            data=sData;
            p = pChild;
        }
        void setData(string& data, CTree * p) {
            sData=data;
            pChild = p;
        }
};
这里的问题是相互依赖,我想可以通过类声明解决它。 做类CTreeNode;在CTree {}类之前。 http://www.codeguru.com/forum/showthread.php?t=383253 我可能正在修改此代码,但它是不完整的,因为多年以来我都不需要写树,但是我认为我已经讲了基础知识。我没有实现operator []。     

要回复问题请先登录注册