开发者

C++ error: request for member 'push_back' in 'v'

开发者 https://www.devze.com 2023-01-24 19:11 出处:网络
Why do I get this error? I am at a loss... error: request for member push_back in v, which is of non-class type std::vector<Leaf, std::allocator<Leaf> >*

Why do I get this error? I am at a loss...

error: request for member push_back in v, which is of non-class type 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);
    }
};


In the function addLeaf() v is a pointer, and leaf is a pointer, you need to dereference both of them.

v->push_back(*leaf);

Also, what's with all the scope qualifications, like Leaf::Leaf and Branch::Branch? It should just be Leaf, and Branch.


v is a pointer to a vector. Use a -> instead of a .. i.e. v->push_back(whatsit)

0

精彩评论

暂无评论...
验证码 换一张
取 消