为什么会出现此错误?我不知所措......
错误:请求push_back
中的成员v
,该类型为非类型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);
}
};
答案 0 :(得分:3)
在函数addLeaf()
中,v是指针,leaf是指针,你需要取消引用它们。
v->push_back(*leaf);
此外,所有范围资格都有,例如Leaf::Leaf
和Branch::Branch
?它应该只是Leaf
和Branch
。
答案 1 :(得分:0)
v
是一个指向矢量的指针。使用->
代替.
。即v->push_back(whatsit)