2015-07-12 131 views
2

我有兩個類TempFileYearTempFile正在獲取過去130年的溫度.txt文件。構造函數讀取此文件並將數據存儲在std::vector<Year>中。現在我想添加一個排序功能,以降序顯示每年的平均溫度。但是當我用struct來定義我的比較(std::sort中的第三個參數)時,我得到的錯誤太多了。我也試圖超載> - 管理員。也沒有工作。C++ std :: sort on std :: vector <Object> - 錯誤太多

class TempFile { 
public: 
    TempFile(std::string temp_file); // read from .txt file and push_back() the data into std::vector<Year> _years. 
    //The vector and constructor are working fine. 

    int get_number_years() const; 

    std::vector<Year> get_vector() const; 
    void sort_descending() const; 
private: 
    std::vector<Year> _years; 
    struct descending 
    { 
     bool operator() (const Year& y1, const Year& y2) 
     { 
      return (y1.get_average_temp() > y2.get_average_temp()); 
     } 
    } temp_descending; 
}; 

int TempFile::get_number_years() const 
{ 
    return _years.size(); 
} 

std::vector<Year> TempFile::get_vector() const 
{ 
    return _years; 
} 

void TempFile::sort_descending() const 
{ 
    std::sort(_years.begin(), _years.end(), temp_descending); 
} 


class Year { 
public: 
    Year(int year, float january, float february, ...); 

    void print() const; 

    float get_added_months() const; 
    float get_average_temp() const; 

    int get_year() const; 

private: 
    int _year; 
    float _january, _february, ...; // all months 

}; 

float Year::get_added_months() const 
{ 
    return (_januar + _februar + _maerz + _april + _mai + _juni + _juli + _august + _september + _oktober 
    + _november + _dezember); 
} 

float Year::get_average_temp() const 
{ 
    return (get_added_months()/12); 
} 

int Year::get_year() const 
{ 
    return _jahreszahl; 
} 

以下是錯誤消息的一個片段:

g++ -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/TempFile.o.d" -o build/Debug/GNU-MacOSX/TempFile.o TempFile.cpp 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:625: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3534:9: error: no viable overloaded '=' 
    __x = _VSTD::move(__y); 
    ~~~^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3852:17: note: in instantiation of function template specialization 'std::__1::swap<const Year>' requested here 
       swap(*__first, *__last); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4037:5: note: in instantiation of function template specialization 'std::__1::__sort<TempFile::absteigend &, const Year *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here 
    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); 
     ^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here 
    std::sort(_years.begin(), _years.end(), temp_absteigend); 
     ^
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const 
class Year { 
    ^
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3621:5: error: no matching function for call to 'swap' 
    swap(*__x, *__y);    // x > y && y <= z 
    ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here 
    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); 
     ^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here 
    std::sort(_years.begin(), _years.end(), temp_absteigend); 
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3598:1: note: candidate template ignored: substitution failure [with _Compare = TempFile::absteigend &, _ForwardIterator = const Year *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3641:9: error: no matching function for call to 'swap' 
     swap(*__x3, *__x4); 
     ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3645:13: error: no matching function for call to 'swap' 
      swap(*__x2, *__x3); 
      ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3671:13: error: no matching function for call to 'swap' 
      swap(*__x3, *__x4); 
      ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3675:17: error: no matching function for call to 'swap' 
       swap(*__x2, *__x3); 
       ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3679:21: error: no matching function for call to 'swap' 
        swap(*__x1, *__x2); 
        ^~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year] 
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year' 
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year' 
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year' 
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year' 
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 
^ 
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3729:5: error: no matching function for call to '__sort3' 
    __sort3<_Compare>(__first, __first+1, __j, __comp); 
    ^~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3866:20: note: in instantiation of function template specialization 'std::__1::__insertion_sort_3<TempFile::absteigend &, const Year *>' requested here 
      _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4037:5: note: in instantiation of function template specialization 'std::__1::__sort<TempFile::absteigend &, const Year *>' requested here 
    __sort<_Comp_ref>(__first, __last, __comp); 
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here 
    _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); 
     ^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here 
    std::sort(_years.begin(), _years.end(), temp_absteigend); 
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3598:1: note: candidate template ignored: substitution failure [with _Compare = TempFile::absteigend &, _ForwardIterator = const Year *] 
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3739:22: error: no viable overloaded '=' 
       *__j = _VSTD::move(*__k); 
       ~~~~^~~~~~~~~~~~~~~~~~ 
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const 
class Year { 
    ^
In file included from TempFile.cpp:8: 
In file included from ./TempFile.h:10: 
In file included from ./Year.h:10: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3742:18: error: no viable overloaded '=' 
      *__j = _VSTD::move(__t); 
      ~~~~^~~~~~~~~~~~~~~~~ 
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const 
class Year { 
    ^
+0

顯示年級,這是什麼錯誤抱怨。 –

+0

您沒有顯示「年份」類,而是根據錯誤消息顯示它具有私人副本分配並且沒有移動分配:您將無法對陣列進行排序而無法移動它們。 –

回答

5

TempFile::sort_descending()const,而你試圖通過std::sort修改_years成員。

從函數的簽名中刪除const說明符或使_years a mutable將解決此問題。

相關問題