I have a C11 std::vector of structures. The structure contains an iterator to another structure of the same type, describing a tree.
Using a forward declaration doesn't work, because vector needs to have the full definition of the structure, but vector can't have the full definition of the structure until definition is complete.
#include <vector>
template <class Payload>
class Tree
{
public:
typedef struct _Node Node;
struct _Node
{
Payload t_payload;
//error: invalid use of incomplete type '_Value_type {aka struct _Node}'
std::vector<Node>::iterator pst_father;
};
std::vector<Node> gast_tree;
};
int main()
{
Tree<int> my_tree;
return 0;
}
in instantiation of 'void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = _Node*]':
required from 'void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _ForwardIterator = _Node*; _Tp = _Node]'
required from 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = _Node; _Alloc = std::allocator<_Node>]'
required from here
error: invalid use of incomplete type '_Value_type {aka struct _Node}'
I want std::vector to serve as container for the Node structure, and I want Node(s) to link with each others to build a tree. It would be trivial using int indexes instead of iterators and resolving the reference later, but I'm trying to learn std::vector<>::iterators. This compiles just fine:
#include <vector>
template <class Payload>
class Tree
{
public:
typedef struct _Node
{
Payload t_payload;
//use a dumb index
int s32_father_index;
} Node;
std::vector<Node> gast_tree;
};
int main()
{
Tree<int> my_tree;
return 0;
}
I tried several ways, but I can't get the iterator to compile. Is it possible to have an iterator to an object inside the object?
Aucun commentaire:
Enregistrer un commentaire