C ++错误中的复合[没有匹配的成员函数来调用'push_back']

时间:2016-04-09 20:48:04

标签: c++ qt design-patterns reference composite

嗨试图做一个简单的复合材料,我在尝试将一个Compenent添加到复合材料中进行培训时遇到了错误

这是代码

组件界面绘制复合的界面

class ObjectInterface
{
public:
ObjectInterface() {}
virtual void draw()=0;
virtual void applyTranslation(float x,float y){}
virtual void applyRotationDirect(float angle){}
virtual void applyRotationIndirect(float angle){}
virtual void applyHomethety(float ratio){}
virtual void applyAxialSymmetry(){}
virtual void applyCentralSymmetry(){}
};


一个元素 - 行

class Line : public ObjectInterface,Object2D
{
public:
    Line(string color,Point p1,Point p2);
    // Inherited method from Object2D
    float getArea();
    float getPerimeter();

    // Inherited method from ObjectInterface
    virtual void draw();
    void applyTranslation(float x,float y);
    void applyRotationDirect(float angle);
    void applyRotationIndirect(float angle);
    void applyHomethety(float ratio);
    void applyAxialSymmetry();
    void applyCentralSymmetry();

    friend ostream& operator<< (ostream &os, const Line &p);
 };


class Fresque : public ObjectInterface
{
public:
    Fresque();
    // Inherited method from ObjectInterface
    void draw();
    void applyTranslation(float x,float y);
    void applyRotationDirect(float angle);
    void applyRotationIndirect(float angle);
    void applyHomethety(float ratio);
    void applyAxialSymmetry();
    void applyCentralSymmetry();

    // Personal method
    bool add(ObjectInterface const &o);
    bool remove(ObjectInterface const& o);

private:
    std::vector<ObjectInterface*> objects;  // CONTAINER FOR COMPOSITE
 };

添加方法的cpp文件

bool Fresque::add(ObjectInterface const & o){
  objects.push_back(o);  //===> THE ERROR HERE
return true;
}

错误:

/fresque.cpp:50:erreur:没有匹配的成员函数来调用'push_back'     objects.push_back(O);     ~~~~~~~~ ^ ~~~~~~~~

IDE是QT,我很难不知道错误在哪里,我很确定这是一个明显的错误:/。

1 个答案:

答案 0 :(得分:2)

std::vector<ObjectInterface*>是指向ObjectInterface的指针的向量。 oObjectInterface,而非ObjectInterface*(指向ObjectInterface的指针),因此您需要获取o的地址:

objects.push_back(&o);