[英]C++ error: request for member 'push_back' in 'v'
为什么会出现此错误? 我很茫然...
错误:请求v
成员push_back
,这是非类类型std::vector<Leaf, std::allocator<Leaf> >*
class Leaf
{
public:
// Variables
std::string *name;
// Methods
Leaf(){}
Leaf(std::string *s)
{
name = s;
}
};
class Branch
{
public:
// Variables
Branch::Branch *parent;
Branch::Branch *child;
std::vector<Leaf> *children;
std::string *name;
// Methods
Branch(std::string *s)
{
children = new std::vector<Leaf>;
name = s;
}
};
class Tree
{
public:
// Variables
Branch::Branch *current;
// Methods
Tree(string *name)
{
current = new Branch::Branch(name);
}
void addBranch(Branch::Branch *newBranch)
{
this->current->child = newBranch;
newBranch->parent = this->current;
}
void addLeaf(Leaf::Leaf *leaf)
{
std::vector<Leaf> *v = this->current->children;
v.push_back(leaf);
}
};
在函数addLeaf()
v是一个指针,而leaf是一个指针,您需要同时取消引用它们。
v->push_back(*leaf);
另外,所有的范围限定条件是什么,例如Leaf::Leaf
和Branch::Branch
? 它应该只是Leaf
和Branch
。
v
是指向向量的指针。 使用->
而不是.
。 即v->push_back(whatsit)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.