我有以下类(class)。 Main类中的for循环期间发生错误。编译器提示draw函数“是非类型的GLCommand”。该应用程序的想法是在同一 vector 中存储许多不同类型的GLCommandShape。我应该采用其他设计方法,还是对这个问题的简单解决方案?

界面:

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

最佳答案

你说的没关系;问题只是运算符的优先级:

(*it)->draw();

10-07 17:58