Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的稳定u排序+;_C++_Stable Sort - Fatal编程技术网

C++ C+中的稳定u排序+;

C++ C+中的稳定u排序+;,c++,stable-sort,C++,Stable Sort,我试图使用稳定的排序来对指针向量进行排序 到某一类。我有这样一个代码: #include <iostream> #include <vector> #include <algorithm> using namespace std; class B { public : B(int y, int j) {x = y, r = j;}; void getVal() {cout << x << e

我试图使用稳定的排序来对指针向量进行排序

到某一类。我有这样一个代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class B
{
    public :
        B(int y, int j) {x = y, r = j;};

        void getVal() {cout << x << endl; };

        int x;
        int r;
};


bool compareB(B* b1, B* b2)
{
    return b1->getVal() < b2->getVal();
}

int main()
{
    B b1(3, 4), b2(-5, 7), b3(12, 111);

    vector<B*> myVec;
    myVec.push_back(&b1);
    myVec.push_back(&b2);
    myVec.push_back(&b3);

    std::stable_sort(myVec.begin(), myVec.end(), compareB);
    for (size_t size = 0; size < myVec.size(); ++size)
    {
        myVec[size]->getVal();
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
B类
{
公众:
B(inty,intj){x=y,r=j;};
void getVal(){cout getVal();
}
int main()
{
b1(3,4),b2(-5,7),b3(12111);
载体myVec;
myVec.推回(&b1);
myVec.push_back(&b2);
myVec.push_back(&b3);
std::stable_sort(myVec.begin()、myVec.end()、compareB);
对于(size_t size=0;sizegetVal();
}
返回0;
}
但是,我在编译时遇到了一个愚蠢的错误:

“错误:“void”和“void”类型的操作数对二进制运算符无效问题在于

void getVal() {cout << x << endl; };
问题在于

void getVal() {cout << x << endl; };

您的
getVal
返回
void
。可能应该是
int
?…并实际返回值,而不仅仅是打印它。您的
getVal
返回
void
。可能应该是
int
?…并实际返回值,而不仅仅是打印它。