我有以下類。在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;
}
你的問題標題不符合您的代碼。你有一個*指針向量*,而不是「抽象類」。 –