C ++错误:请求'v'中的成员'push_back'

时间:2010-11-14 03:58:47

标签: c++ oop

为什么会出现此错误?我不知所措......

错误:请求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);
    }
};

2 个答案:

答案 0 :(得分:3)

在函数addLeaf()中,v是指针,leaf是指针,你需要取消引用它们。

v->push_back(*leaf);

此外,所有范围资格都有,例如Leaf::LeafBranch::Branch?它应该只是LeafBranch

答案 1 :(得分:0)

v是一个指向矢量的指针。使用->代替.。即v->push_back(whatsit)