2017-04-13 66 views
0

C++中的新手。我想創建一個動態對象數組,並使用std :: sort()對它們進行排序。但是,出現了一些錯誤,我無法弄清楚原因。謝謝你的幫助。 中出現錯誤是這樣的:iterator_category':不是任何基類的成員'std :: iterator_traits'<_InIt>'

> 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; 
    } 
} 
+0

您的錯誤消息,在那麼結束,應該告訴你什麼行代碼產生了這個混亂。那是什麼? – NathanOliver

+0

'sort(ProblemSet [0],ProblemSet [N-1]);'是錯誤的。索引到一個未知大小的數組是不會產生迭代器的。你可能意思是'排序(ProblemSet,ProblemSet + N);' – WhiZTiM

+0

錯誤信息基本上說「Product'不是迭代器」。你把'產品'傳給'排序'。 – molbdnilo

回答

0
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]);是錯誤的。索引到一個未知大小的數組是不會產生所要求的std::sort迭代器(你可能是指

sort(ProblemSet, ProblemSet + N); 

你也可能需要更換您的手動動態數組管理:

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

隨着std::vector

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

這樣做甚至可以簡化您的功能接口。並排序:

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; 
    } 
} 
+0

非常感謝!這完全解決了我的問題,並感謝您的建議,以簡化我的代碼。它確實有很大的幫助。 – RainbowRain