我有以下类。在Main
类的for循环中发生错误。编译器抱怨draw
函数“属于非GLCommand类型”。应用程序的想法是在相同的向量中存储许多不同类型的GLCommand
和Shape
。我应该采取不同的设计方法,还是他们对这个问题的简单解决方案?通过抽象类的向量进行迭代
接口:
class GLCommand
{
public:
GLCommand();
virtual ~GLCommand();
virtual void draw() = 0;
};
抽象类:
class Shape : public GLCommand
{
public:
Shape(int);
virtual ~Shape();
virtual void draw() {};
private:
double colour[];
int sides;
};
派生类:
class Polygon : public Shape
{
public:
Polygon(int sides);
virtual ~Polygon();
void draw();
private:
vector<Coordinates *> verticies;
};
主要:
int main()
{
vector <GLCommand*> vec;
Polygon p(4);
vec.push_back(&p);
for (vector<GLCommand*>::iterator it = vec.begin(); it!=vec.end(); ++it)
{
*it->draw();
}
return 0;
}
你的问题标题不符合您的代码。你有一个*指针向量*,而不是“抽象类”。 –