迭代通过抽象类的向量

nf3*_*743 2 c++ abstract-class stl

我有以下课程.在Main类中的for循环期间发生错误.编译器抱怨draw函数"是非类型GLCommand".应用程序的想法是存储许多不同类型的GLCommand和Shape相同的载体中.我应该采用不同的设计方法,还是简单解决这个问题?

接口:

class GLCommand
{
    public:
        GLCommand();
        virtual ~GLCommand();
    virtual void draw() = 0;
};
Run Code Online (Sandbox Code Playgroud)

抽象类:

class Shape : public GLCommand
{
public:
  Shape(int);
  virtual ~Shape();
  virtual void draw() {};
private:
  double colour[];
  int sides;

};
Run Code Online (Sandbox Code Playgroud)

派生类:

class Polygon : public Shape
{
  public:
    Polygon(int sides);
    virtual ~Polygon();

    void draw();

private:
  vector<Coordinates *> verticies;

};
Run Code Online (Sandbox Code Playgroud)

主要:

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;
}
Run Code Online (Sandbox Code Playgroud)

Ker*_* SB 8

你说什么都没有关系; 问题只是运算符优先级:

(*it)->draw();
Run Code Online (Sandbox Code Playgroud)