简体   繁体   English

错误:&#39;类 std::vector<Shape*> &#39; 没有名为 &#39;sort&#39; 的成员

[英]error: ‘class std::vector<Shape*>’ has no member named ‘sort’

I have a code where I am drawing different Shape , now there can be preference on which one to draw first among say Square, rectangle and triangle .我有一个代码,我在其中绘制了不同的 Shape ,现在可以优先选择在 Square、 rectangle 和 triangle 中首先绘制哪个。 I have override the "<" ( less than operator for base class Shape. My preference of drawing is in increasing order as mentioned in typeOrderTable. Therefore triangle will be drawn before Square But upon compilation I get the error error: 'class std::vector' has no member named 'sort'.我已经覆盖了“<”(小于基类 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;
}

There is no std::vector<...>::sort .没有std::vector<...>::sort Instead, you will need to use std::sort :相反,您需要使用std::sort

std::sort(orderedList.begin(), orderedList.end());

However , this will try to compare the pointers in your std::vector<Shape*> (hint: refactor this if at all possible).但是,这将尝试比较std::vector<Shape*>指针(提示:如果可能,请重构它)。 Instead, you will need to pass a custom comparator that dereferences the pointers:相反,您需要传递一个取消引用指针的自定义比较器:

std::sort(
    orderedList.begin(),
    orderedList.end(),
    [](Shape *a, Shape *b) { return *a < *b; }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:'类 std::vector <route, std::allocator<route> &gt;' 没有名为“exitPoint”的成员</route,> - error: ‘class std::vector<route, std::allocator<route> >’ has no member named ‘exitPoint’ C ++编译器:'class std :: vector <std :: vector <char >>>'没有名为'emplace_back'的成员 - C++ compiler: 'class std::vector<std::vector<char> >' has no member named 'emplace_back' 错误:“类std :: stack &lt;&gt;”没有名为“ pop_back”的成员 - error: 'class std::stack<>' has no member named 'pop_back' g++:'类标准::map <int, std::vector<std::vector<int> &gt; &gt;' 没有名为 'insert_or_assign' 的成员</int,> - g++: 'class std::map<int, std::vector<std::vector<int> > >' has no member named 'insert_or_assign' std :: array错误:没有名为&#39;assign&#39;的成员 - std::array error: Has no member named 'assign' 错误:&#39;类 std::unique_ptr <std::set<long unsigned int> &gt;&#39; 没有名为 &#39;size&#39; 的成员 - error: 'class std::unique_ptr<std::set<long unsigned int> >' has no member named 'size' flann / util / serialization.h类std :: unordered_map <unsigned int, std::vector<unsigned int> &gt;&#39;没有名为&#39;serialize&#39;的成员 - flann/util/serialization.h class std::unordered_map<unsigned int, std::vector<unsigned int> >' has no member named 'serialize' “的std ::矢量 <double> :: iterator&#39;没有名为&#39;begin&#39;的成员 - 'std::vector<double>::iterator' has no member named 'begin' 我得到 std class 向量对于我的所有向量方法没有成员错误 - I'm getting std class vector has no member error for all my vector methods 与class一起使用std :: reference_wrapper时编译错误“没有成员命名” - compile error with std::reference_wrapper “has no member named” when used with class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM