简体   繁体   English

使用排序 function 和 STL 列表 c++ 时遇到问题,我该如何改进?

[英]Having troubles using sort function with STL List for c++, how can i improve it?

This is all in the header file, and in main its pretty simple, I declare a list and then populate it, with no issues, and can print the list from another function that I have in the header file no problems, but the issue comes with the sort function.这一切都在 header 文件中,主要是它非常简单,我声明一个列表然后填充它,没有问题,并且可以从另一个 function 打印列表,我在 Z099FB9953460DBF1C7E39F6E 文件中遇到问题,但问题出现了问题 594排序为 function。 It keeps saying that it它一直说它

Severity Code Description Project File Line Suppression State Error C2664 'bool (Bike *,Bike *)':严重性代码描述项目文件行抑制 State 错误 C2664 'bool (Bike *,Bike *)':
cannot convert argument 2 from '_Value_type' to 'Bike *' Project7 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xutility 1481无法将参数 2 从 '_Value_type' 转换为 'Bike *' Project7 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xutility 1481

//the argument for the sort function

bool comp_id(Bike* b1, Bike* b2)
{
    return b1->id_num < b2->id_num;
}


//This is how I plan on sorting it.
// Also, side note can I do list<Bike> secondList = head; and then sort secondList.sort(comp_id)?

        head.sort(comp_id);
        for (auto iterator = head.begin(); iterator != head.end(); ++iterator)
        {
            cout << iterator->id_num << endl;
        }

//This is the Structure 

struct Bike
{
    char manufact[25];
    int id_num;
    status rented_code; //RENTED/NOT_RENTED
    char to_whom[25];   //to whom bike is rented
    int size;
    float cost_per_day;
    bool deleted;   //to mark bike as deleted in the list.
    Bike* next_manuf;   //pointer to next node in the
                //manufacturers list
    Bike* next_id;  //pointer to the next node
            //in the list by ID
    Bike* next; //pointer to the next node in the general list
};


If head is a list<Bike> your compare function is wrong, it must be如果headlist<Bike>你的比较 function 是错误的,它一定是

bool comp_id(Bike & b1, Bike & b2)
{
  return b1.id_num < b2.id_num;
}

or better或更好

bool comp_id(const Bike & b1, const Bike & b2)
{
  return b1.id_num < b2.id_num;
}

Your compare function would be ok if head was a list<Bike *>如果head列表<Bike *> ,您的比较 function 就可以了

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM