我在传递对 Vector 的引用时仍然遇到问题
我在 this 发帖并从回复中学习,但
我仍然无法让它工作。
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;
}
带星号的行不起作用。 “tree_dict = new Dictionary”
错误是:
“没有运算符”=匹配这些操作数。
我想做的是每当
实例化新树时创建一个新的 Node*s 向量。传递对新向量的引用(tree_dict) 到 Node
构造函数,它将将该引用传递给 Node 的每个新实例
可以将指针推回自身
(Node* left 和 Node* right),在将引用传递给其子 Node
之前,该实例 。是一个包含指向
树中每个 Node* 的指针的向量,我需要一些帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这表示“在堆上分配一个新的 Dictionary 对象,并将指向它的指针存储在
tree_dict
中”。不幸的是,tree_dict
不是一个指针。这表示“创建一个新的 Dictionary 对象,并将其复制到
tree_dict
中。”That says "Allocate a new Dictionary object on the heap, and store a pointer to it in
tree_dict
". Unfortunately,tree_dict
is not a pointer.This says "Create a new Dictionary object, and copy it into
tree_dict
."天哪,你的代码有很多问题。您可能应该阅读一本 C++ 书籍来学习基础知识,因为即使您的代码是可编译的,它的实现也非常糟糕。我必须指出的一件事似乎没有人提到过
std::vector& 的声明node_dict;
你不能声明这样的引用。参考必须是作业。您说
node_dict
是对std::vector
对象的引用,但没有告诉它它引用的是什么。如果编译成功,那么您的编译器将提取 &符号而不是像 ti 那样抛出错误。至于你的代码的糟糕之处,为什么你还要将 node_dict 声明为类变量?您可以在构造函数中为其分配一个值,但不要在构造函数之外使用它。没有理由它应该是一个类变量。
Holy hell there's a lot wrong with your code. You should probably go through a begging C++ book to learn the basics because even if your code was compilable, it's very very poorly implemented. The one thing I have to point out that no one seems to have mentioned is the
declaration of
std::vector<Node*>& node_dict;
You can't declare a reference like that. A reference HAS to be an assignment. You're saying
node_dict
is a reference to a astd::vector<Node*>
object, but not telling it what it's referencing. If this compiles then your compiler is pulling out the & symbol instead of throwing an error like ti should.As for the poorness of your code, why are you even declaring node_dict as a class variable? You assign it a value in your constructor, but then don't use it outside of your constructor. There's no reason it should be a class variable.
如果在 C++ 中新建一个类型,您将获得一个指向堆分配对象的指针。如果要就地分配,请编写不带 new 关键字的构造函数。
If you new a type in C++, you get a pointer to a heap allocated object. If you want to allocate it in place, write the constructor without the new keyword.
这应该简单地是:
tree_dict不是指针,您正在按值存储向量。
请注意,正如发布的那样,您至少泄漏了
root
的内存,因为Tree
没有删除它的析构函数。或者,您可以使用智能指针,例如std::auto_ptr
,它会删除它会自动为您服务,并有助于使您的代码异常安全:对于
tree_dict
也是如此,它最好作为 Boosts 或 TR1shared_ptr
或类似 Boostsptr_vector
。That should simply be:
tree_dict
is not a pointer, you are storing thevector
by value.Note that as posted you are at least leaking the memory for
root
asTree
doesn't have a destructor which deletes it. Alternatively you can use smart pointers likestd::auto_ptr
which deletes it automatically for you and helps making your code exception-safe:Same goes for the
tree_dict
which would be better off as avector
of e.g. Boosts or TR1shared_ptr
or something like Boostsptr_vector
.