C++ 标准::排序 标准::矢量<Object> - 错误太多

C++ std::sort on std::vector<Object> - Too many Errors

本文关键字:标准 太多 gt 错误 lt 排序 矢量 C++ Object      更新时间:2023-10-16

我有两个类TempFileYearTempFile正在获取一个。txt文件,其中包含过去130年的温度。构造函数读取该文件并将数据存储在std::vector<Year>中。现在我想添加一个排序函数,它按降序显示每年的平均温度。但是,当我做一个struct来定义我的比较(std::sort中的第三个参数),我得到太多的错误。我也尝试过重载> -操作符。也没用

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 {
      ^

TempFile::sort_descending()被标记为const,而您试图通过std::sort修改_years成员。

const指示符从函数签名中删除或将_years改为mutable将解决此问题。