C++错误中的复合 [没有匹配调用 'push_back'的成员函数]
Composite in C++ error [no matching member function to callto 'push_back']
嗨,我正在尝试做一个简单的合成,当我试图向合成中添加一个Compener以训练时,我遇到了一个错误
这是代码
组件接口绘制复合物的接口
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:erreur:调用"push_back"时没有匹配的成员函数objects.push_back(o);~~~~~~
IDE是QT,我不知道错误在哪里,感觉很糟糕,我很确定这是一个明显的错误:/。
std::vector<ObjectInterface*>
是指向ObjectInterface
s的指针向量。o
是ObjectInterface
,而不是ObjectInterface*
(指向ObjectInterface
的指针),因此您需要获得o
:的地址
objects.push_back(&o);
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 我们可以访问一个不存在的联盟的成员吗