У меня есть код, в котором я рисую разные фигуры, теперь можно выбрать, какой из них рисовать первым, например, среди квадратов, прямоугольников и треугольников. Я переопределил "<" (меньше, чем оператор для базового класса Shape. Мои предпочтения рисования в порядке возрастания, как указано в typeOrderTable. Поэтому треугольник будет отрисован перед Square, но при компиляции я получаю ошибку ошибки: 'class std :: У вектора 'нет члена с именем' sort '. </p>
#include <iostream>
#include <vector>
#include <typeinfo>
#include <string.h>
using namespace std;
class Shape
{
public:
virtual void Draw() const = 0;
virtual bool Precedes(const Shape&) const ;
bool operator<(const Shape& s)
{
return Precedes(s);
}
private:
static char* typeOrderTable[];
};
char* Shape::typeOrderTable[] = {"Rectangle","Square","Triangle",0 };
bool Shape::Precedes(const Shape& s) const
{
const char* thisType = typeid(*this).name();
const char* argType = typeid(s).name();
bool done = false; int thisOrd = -1;
int argOrd = -1;
for (int i=0; !done; i++)
{
const char* tableEntry = typeOrderTable[i];
if (tableEntry != 0)
{
if (strcmp(tableEntry, thisType) == 0)
thisOrd = i;
if (strcmp(tableEntry, argType) == 0)
argOrd = i;
if ((argOrd > 0) && (thisOrd > 0))
done = true;
}
else // table entry == 0
done = true;
}
return thisOrd < argOrd;
}
class Square : public Shape
{
public:
virtual void Draw() const
{
std::cout << "Inside Draw of Square \n" ;
}
};
class Rectangle : public Shape
{
public:
virtual void Draw() const
{
std::cout << "Inside Draw of Rectangle \n" ;
}
};
class Triangle : public Shape
{
public:
virtual void Draw() const
{
std::cout << "Inside Draw of Triangle \n" ;
}
};
void DrawAllShapes(std::vector<Shape*>& list)
{
std::vector<Shape*> orderedList = list;
orderedList.sort();
std::vector<Shape*>::iterator it =orderedList.begin();
for (;it!=orderedList.end(); ++it)
(*it)->Draw();
}
int main()
{
std::vector<Shape*> vec;
vec.push_back(new Triangle);
vec.push_back(new Square);
vec.push_back(new Rectangle);
vec.push_back(new Square);
DrawAllShapes(vec);
return 0;
}