繁体   English   中英

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

[英]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::LeafBranch::Branch 它应该只是LeafBranch

v是指向向量的指针。 使用->而不是. v->push_back(whatsit)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM