[英]error: ‘class std::vector<route, std::allocator<route> >’ has no member named ‘exitPoint’
[英]error: ‘class std::vector<Shape*>’ has no member named ‘sort’
我有一個代碼,我在其中繪制了不同的 Shape ,現在可以優先選擇在 Square、 rectangle 和 triangle 中首先繪制哪個。 我已經覆蓋了“<”(小於基類 Shape 的運算符。我對繪圖的偏好是按 typeOrderTable 中提到的遞增順序。因此三角形將在 Square 之前繪制但在編譯時我收到錯誤錯誤:'class std:: vector' 沒有名為 'sort' 的成員。
#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;
}
沒有std::vector<...>::sort
。 相反,您需要使用std::sort
:
std::sort(orderedList.begin(), orderedList.end());
但是,這將嘗試比較std::vector<Shape*>
的指針(提示:如果可能,請重構它)。 相反,您需要傳遞一個取消引用指針的自定義比較器:
std::sort(
orderedList.begin(),
orderedList.end(),
[](Shape *a, Shape *b) { return *a < *b; }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.