I asked a similar question in this posting and learned from the replys but
I still can't get it to work.
test_vector.h
#include <vector>
class Node
{
public:
std::vector<Node*>& node_dict;
int depth;
char* cargo;
Node* left;
Node* right;
Node( int a_depth, std::vector<Node*>& a_dict);
~Node();
};
class Tree
{
public:
std::vector<Node*>tree_dict;
Node* root;
Tree();
Tree(const Tree &original);
};
test_vector.cpp
#include "test_vector.h"
using namespace std;
typedef std::vector<Node*>Dictionary;//This seems like a good idea.
typedef std::vector<Tree*>Population;
Population pop;
int Tree_depth = 3;
Node::Node( int a_depth, std::vector<Node*>&a_dict):node_dict(a_dict), depth(a_depth)
{
if (depth <= 0)
{
cargo = "leaf_Node";
left = 0;
right = 0;
node_dict.push_back(this);
return;
}
else;
{
cargo = "Tree_Node";
node_dict.push_back(this);
depth--;
left = new Node(depth, node_dict);
right = new Node(depth, node_dict);
}
return;
};
Node::~Node()
{
delete left;
delete right;
};
Tree::Tree():tree_dict(NULL)
{
****tree_dict = new Dictionary;****
root = new Node(Tree_depth, tree_dict);
};
//copy constructor
Tree::Tree(const Tree &original):tree_dict(NULL)
{
root = NULL;
root = new Node (*(original.root));
};
int main()
{
for (int i = 0;i <= 3; i++)
{
pop.push_back(new Tree());
}
return 0;
}
The line with the asterisks doesn't work. "tree_dict = new Dictionary"
the error is:
"no operator "=" matches these operands.
What I'm trying to do is create a new vector of Node*s whenever a new Tree is
instantiated. Pass a reference to the new vector (tree_dict) to the Node
constructor, which will pass that reference to each new instance of Node
(Node* left and Node* right) which can push_back a pointer to themselves before
passing the reference on to their child Nodes.
So each Tree.tree_dict is a single vector containing pointers to each Node* in
the tree. I need some help please.