c++ - Composite in C++ 错误 [没有匹配的成员函数来调用 'push_back']

标签 c++ qt design-patterns reference composite

您好,我想做一个简单的复合,但在尝试将一个组件添加到复合中进行训练时,我遇到了一个错误

这是代码

Component interface Component绘制接口(interface)

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
 };

add 方法的 cpp 文件

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

错误:

/fresque.cpp:50: 错误:没有匹配的成员函数来调用“push_back” objects.push_back(o); ~~~~~~~~^~~~~~~~~

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

最佳答案

std::vector<ObjectInterface*>是指向 ObjectInterface 的指针 vector 秒。 o是一个 ObjectInterface ,不是 ObjectInterface* (指向 ObjectInterface 的指针),因此您需要获取 o 的地址:

objects.push_back(&o);

关于c++ - Composite in C++ 错误 [没有匹配的成员函数来调用 'push_back'],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522518/

相关文章:

c++ - 为什么不从临时对象(operator+ 的结果)中移动不调用构造函数?

c++ - Qt:按值将自定义对象存储在集合中

python - PyQt:使用 QApplication.quit 时偶尔出现段错误

javascript - 避免 namespace 污染的模式

go - 如何实现一个channel和多个reader同时读取相同的数据?

C++模板成员函数错误

c++ - 使用 ifstream 将二进制数据读入结构

c++ - 通过引用 CUDA 指针进行 CUDA 矩阵求逆

java - Netbeans 中的 GUI 设计器 [Qt 或 GTK]

c++ - 在 C++ 中获取工厂类的用户输入