2012-06-01 7 views
0

트리를 만들려고합니다. 내 첫 번째 요구 사항은 트리 수준의 개체가 삽입 순서에 따라 정렬된다는 것입니다.부스트 멀티 인덱스 컨테이너를 사용하여 트리를 만드는 방법

#include <vector> 
#include <iostream> 
#include <string> 

class Node; 
typedef std::vector<Node> NodeList; 

class Node 
{ 
public: 
    Node(const std::string& name, int val=0): name_(name), value_(val), list_() {} 
    Node(const Node& nd): name_(nd.name_), value_(nd.value_), list_(nd.list_) {} 
    Node& operator=(const Node& nd) 
    { 
     name_ = nd.name_; 
     value_ = nd.value_; 
     list_ = nd.list_; 
     return *this; 
    } 
    const std::string& name(void) const {return name_;} 
    NodeList& list(void) {return list_;} 
    const NodeList& list(void) const {return list_;} 
    int value(void) const {return value_;} 
    void value(int val) {value_ = val;} 
private: 
    std::string name_; 
    int value_; 
    NodeList list_; 
}; 

std::ostream& operator<<(std::ostream& os, const Node& nd) 
{ 
    os << nd.name() << "=\"" << nd.value() << "\""; 
    return os; 
} 

std::ostream& operator<<(std::ostream& os, const NodeList& ndlst) 
{ 
    for(NodeList::const_iterator it = ndlst.begin(); it < ndlst.end(); it++) 
    { 
     os << ' ' << *it; 
    } 
    return os; 
} 

int main() 
{ 
    std::cout << "Creating a little tree..." << std::endl; 
    Node a("ROOT", 1); 
    a.list().push_back(Node("LEVEL1_1", 21)); 
    a.list().push_back(Node("LEVEL1_2", 22)); 
    a.list().push_back(Node("LEVEL1_3", 23)); 
    a.list().push_back(Node("LEVEL1_4", 24)); 
    a.list().at(2).list().push_back(Node("LEVEL2_1", 231)); 
    std::cout << "Done. Now let's see some values." << std::endl; 
    std::cout << "root: " << a.value() << ", root->node2: " 
       << a.list().at(1).value() 
       << ", root->node3->node1: " 
       << a.list().at(2).list().at(0).value() 
       << std::endl; 
    std::cout << "Ok, now let's use our '<<' operators." << std::endl; 
    std::cout << a << std::endl; 
    std::cout << a.list() << std::endl; 
    return 0; 
} 

이 작은 프로그램은 OK 컴파일하고 예상대로 실행 :

는 첫 번째 시도로서 나는 STL 벡터 클래스를 사용했다. 다음과 같이 한 단계 더으로

, 나는 그것의 이름으로 나무 수준의 개체에 액세스 할 수있는 기능을 추가하고 싶습니다, 그래서 부스트 다중 인덱스를 사용하여 tryed은 그래서 노드 클래스를 수정 :

class Node; 
struct Name{}; 


typedef boost::multi_index::multi_index_container< 
    Node, 
    boost::multi_index::indexed_by< 
     boost::multi_index::random_access<>, 
     boost::multi_index::hashed_non_unique< 
      boost::multi_index::tag<Name>, 
      boost::multi_index::const_mem_fun<Node, const std::string&, &Node::name> 
     > 
    > 
> NodeList; 


class Node 
{ 
public: 
    Node(const std::string& name, int val=0): name_(name), value_(val), list_() {} 
    Node(const Node& nd): name_(nd.name_), value_(nd.value_), list_(nd.list_) {} 
    Node& operator=(const Node& nd) 
    { 
     name_ = nd.name_; 
     value_ = nd.value_; 
     list_ = nd.list_; 
     return *this; 
    } 
    const std::string& name(void) const {return name_;} 
    int value(void) const {return value_;} 
    void value(int val) {value_ = val;} 
    NodeList& list(void) {return list_;} 
    const NodeList& list(void) const {return list_;} 
private: 
    std::string name_; 
    int value_; 
    NodeList list_; 
}; 

그러나 그것은 전혀 컴파일되지 않습니다. 바로 그 첫 번째 오류는 GNU g ++ 버전 4.6.1되어 사용

tree.cpp:19:65: error: incomplete type 'Node' used in nested name specifier 

은 누군가가 나이 상호 의존도를 해결하는 데 도움이 수 있습니까?

대단히 감사합니다.

#include <boost/multi_index_container.hpp> 
#include <boost/multi_index/random_access_index.hpp> 
#include <boost/multi_index/hashed_index.hpp> 
#include <boost/multi_index/identity.hpp> 
#include <boost/multi_index/global_fun.hpp> 
#include <iostream> 
#include <string> 

class Node; 
struct Name{}; 
const std::string& getNodeName(const Node& nd); 


typedef boost::multi_index::multi_index_container< 
    Node, 
    boost::multi_index::indexed_by< 
     boost::multi_index::random_access<>, 
     boost::multi_index::hashed_non_unique< 
      boost::multi_index::tag<Name>, 
      boost::multi_index::global_fun<const Node&, const std::string&, &getNodeName> 
     > 
    > 
> NodeList; 


class Node 
{ 
public: 
    Node(const std::string& name, int val=0): name_(name), value_(val), list_(new NodeList) {} 
    Node(const Node& nd): name_(nd.name_), value_(nd.value_), list_(new NodeList(*nd.list_)) {} 
    virtual ~Node() {delete list_;} 
    Node& operator=(const Node& nd) 
    { 
     name_ = nd.name_; 
     value_ = nd.value_; 
     *list_ = *nd.list_; 
     return *this; 
    } 
    const std::string& name(void) const {return name_;} 
    int value(void) const {return value_;} 
    void value(int val) {value_ = val;} 
    NodeList& list(void) {return *list_;} 
    const NodeList& list(void) const {return *list_;} 
    friend const std::string& getNodeName(const Node& nd); 
private: 
    std::string name_; 
    int value_; 
    NodeList* list_; 
}; 



const std::string& getNodeName(const Node& nd) 
{ 
    return nd.name_; 
} 



std::ostream& operator<<(std::ostream& os, const Node& nd) 
{ 
    os << nd.name() << "=\"" << nd.value() << "\""; 
    return os; 
} 



std::ostream& operator<<(std::ostream& os, const NodeList& ndlst) 
{ 
    for(NodeList::const_iterator it = ndlst.begin(); it < ndlst.end(); it++) 
    { 
     const Node& nd = *it; 
     os << ' ' << nd; 
    } 
    return os; 
} 



int main() 
{ 
    std::cout << "Creating a little tree..." << std::endl; 
    Node a("ROOT", 1); 
    a.list().push_back(Node("LEVEL1_1", 21)); 
    a.list().push_back(Node("LEVEL1_2", 22)); 
    a.list().push_back(Node("LEVEL1_3", 23)); 
    a.list().push_back(Node("LEVEL1_4", 24)); 
    // a.list().at(2).list().push_back(Node("LEVEL2_1", 231)); // First call to "at()" returns a read-only iterador so I can't append elements to its pointed object. 
    std::cout << "Done. Now let's see some values." << std::endl; 
    std::cout << "root: " << a.value() << ", root->node2: " 
       << a.list().at(1).value() << std::endl; 
      /* << ", root->node3->node1: " << a.list().at(2).list().at(0).value() 
       << std::endl; */ 
    std::cout << "Ok, now let's use our '<<' operators." << std::endl; 
    std::cout << a << std::endl; 
    std::cout << a.list() << std::endl; 
    return 0; 
} 

가 지금은 멀티에 의해 검색된 객체에 요소를 추가하기 위해 수정/교체 방법에서 작동해야합니다

안부

제임스 Hopkin에

덕분에, 나는 부분적으로 작업 프로그램이 -index itrerators.

대단히 감사합니다.

+0

'NodeBase'를 어딘가에 정의 했습니까? Node Node가 NodeBase에서 상속 받았음을 알지도 못했다. – mistapink

+0

Ups, 이것은 기본 클래스의 노드리스트를 사용하여 해결하려는 실패한 복사 복사 오류이다. 편집 및 수정. – Patxitron

답변

2

typedef에서 Node::name을 사용하면 오류가 발생한 것 같습니다. 어쩌면 비회원 함수를 만들어 Node의 이름에 액세스하여 대신 사용할 수 있습니다.

0

Boost.PropertyTree를 살펴 보셨습니까?

+0

나는 그것을 살펴볼 것이다. 감사. – Patxitron