2010-11-14 48 views
0

爲什麼我得到這個錯誤?我很茫然...C++錯誤:'v'中成員'push_back'的請求

錯誤:請求用於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); 
    } 
}; 

回答

3

的在功能addLeaf() v是一個指針,和葉是一個指針,你需要對它們解除引用。

v->push_back(*leaf); 

而且,什麼是與所有範圍的資格,像Leaf::LeafBranch::Branch?它應該是LeafBranch

0

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

相關問題