Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a vector of abstract classes

I have the following classes. An error occurs during the for loop in the Main class. The compiler complains about the draw function "is of non-type GLCommand". The idea of the application is to store many different types of GLCommand and Shape within the same vector. Should I take a different design approach, or is their a simple fix to this problem?

Interface:

class GLCommand
{
    public:
        GLCommand();
        virtual ~GLCommand();
    virtual void draw() = 0;
};

Abstract Class:

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

};

Derived class:

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

    void draw();

private:
  vector<Coordinates *> verticies;

};

Main:

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;
}
like image 767
nf313743 Avatar asked Feb 02 '26 15:02

nf313743


1 Answers

Nothing you said is relevant; the problem is just operator precedence:

(*it)->draw();
like image 127
Kerrek SB Avatar answered Feb 05 '26 05:02

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!