简体   繁体   中英

iterator_category': is not a member of any base class of 'std::iterator_traits<_InIt>'

Newbie in C++. I would like to create a dynamic object array and use std::sort() to sort them. However, several errors appear and I could not figure out why. Thanks for any help. The errors appear like this:

 > community\\vc\\tools\\msvc\\14.10.25017\\include\\xutility(988): > error C2794: 'iterator_category': is not a member of any direct or > indirect base class of 'std::iterator_traits<_InIt>' > with > [ > _InIt=Problem > ] \\include\\algorithm(2915): > note: see reference to function template instantiation 'void > std::_Debug_range<_RanIt>(_InIt,_InIt,std::_Dbfile_t,std::_Dbline_t)' > being compiled > with > [ > _RanIt=Problem, > _InIt=Problem 
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Problem{
public:
    string name;
    int t;
    int d;
    Problem() {}
    Problem(string name,int t,int d):name(name),t(t),d(d) {}
    ~Problem() {}
    bool operator<(const Problem &right) const {
        if (t == right.t) return d < right.d;
        else return t < right.t;
    }
};

void FindOrder(int H, int N, int t0, Problem ProblemSet[]);
bool compare(const Problem &left,const Problem &right) {
    if (left.t == right.t) return left.d < right.d;
    else return left.t < right.t;
}

int main()
{
    int H, N, t0;
    cin >> H;
    while (H >= 0) {
        cin >> N >> t0;
        //Problem ProblemSet = (Problem)malloc(N * sizeof(struct ProblemNode));
        Problem* ProblemSet = new Problem[N];
        for (int i = 0;i<N;i++)
            cin >> ProblemSet[i].name >> ProblemSet[i].t >> ProblemSet[i].d;
        FindOrder(H, N, t0, ProblemSet);
        delete[] ProblemSet;
        cin >> H;
    }
    return 0;
}

void FindOrder(int H, int N, int t0, Problem ProblemSet[]) {
    int total = t0;
    sort(ProblemSet[0], ProblemSet[N-1]);
    for (int i = 0;i < N;i++) {
        cout << ProblemSet[i].name << ProblemSet[i].t << ProblemSet[i].d << endl;
    }
}
void FindOrder(int H, int N, int t0, Problem ProblemSet[]) {
    int total = t0;
    sort(ProblemSet[0], ProblemSet[N-1]);   //Wrong
    for (int i = 0;i < N;i++) {
        cout << ProblemSet[i].name << ProblemSet[i].t << ProblemSet[i].d << endl;
    }
}

sort(ProblemSet[0], ProblemSet[N-1]); is wrong. indexing into an array of an unknown size is will not produce an iterator (as required by std::sort . You probably meant

sort(ProblemSet, ProblemSet + N);

You may also want to replace your manual dynamic array management:

Problem* ProblemSet = new Problem[N];
....
delete[] ProblemSet;

With a std::vector .

std::vector<Problem> ProblemSet(N);

Doing that would even simplify your function interfaces. And to sort:

void FindOrder(int H, int t0, std::vector<Problem>& ProblemSet) {
    int total = t0;
    sort(ProblemSet.begin(), ProblemSet.end());
    for (auto& p : ProblemSet) {
        cout << p.name << p.t << p.d << endl;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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