为什么会出现此错误?我很茫然...

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

最佳答案

在函数addLeaf()中,v是一个指针,而leaf是一个指针,您需要同时取消引用它们。

v->push_back(*leaf);


另外,所有范围限定符(如Leaf::LeafBranch::Branch)又如何?它应该只是LeafBranch

关于c++ - C++错误:请求“v”中的成员“push_back”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4176130/

10-13 04:58