mercredi 15 juin 2022

C++ weird error appears when I try to sort std:;vector of custom objects [duplicate]

My assignment today involves generating a set of fractions whose value are in the range [0, 1], sort them and print out the k-th smallest one. (Set here means everything is unique.). However I'm stuck on the generation part, because every time I try to sort it throws out this massive error log. I really don't know what to do, can anyone help me?

The full code:

#include <bits/stdc++.h>
using namespace std;
struct phanso{
    int tu;
    int mau;
    phanso(int tu, int mau){
        int k = __gcd(tu, mau);
        this->tu = int(tu / k);
        this->mau = int(mau / k);
    }
    double value(){
        return double(double(tu) / double(mau));
    }
    void print(){
        cout << this->tu  << "/" << this->mau << endl;
    }
};

bool comp(phanso a, phanso b){
    return a.value() < b.value();
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    vector<phanso> F;
    F.push_back(phanso(0, 1));
    F.push_back(phanso(1, 1));
    int n, k; cin >> n >> k;
    for (int i = 1; i < n; i++){
        for (int j = i + 1; j <= n; j++){
            F.push_back(phanso(i, j));
        }
    }
    sort(F.begin(), F.end(), comp);
    //bo het cacphan tu bi lap
    auto last = unique(F.begin(), F.end());
    F.erase(last, F.end());
    for (int i = 0; i < F.size(); i++){
        cout << "F[" << i << "] = ";
        F[i].print();
    }
}

The error (in its full glory):

In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from main.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equal_to_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<phanso*, std::vector<phanso> >; _Iterator2 = __gnu_cxx::__normal_iterator<phanso*, std::vector<phanso> >]’:
/usr/include/c++/9/bits/stl_algo.h:980:20:   required from ‘_ForwardIterator std::__unique(_ForwardIterator, _ForwardIterator, _BinaryPredicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<phanso*, std::vector<phanso> >; _BinaryPredicate = __gnu_cxx::__ops::_Iter_equal_to_iter]’
/usr/include/c++/9/bits/stl_algo.h:1010:27:   required from ‘_FIter std::unique(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<phanso*, std::vector<phanso> >]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',37)">main.cpp:37:42</span>:   required from here
/usr/include/c++/9/bits/predefined_ops.h:106:23: error: no match for ‘operator==’ (operand types are ‘phanso’ and ‘phanso’)
  106 |       { return *__it1 == *__it2; }
      |                ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:883:5: note: candidate: ‘template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)’
  883 |     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:883:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from main.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h:106:23: note:   ‘phanso’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
  106 |       { return *__it1 == *__it2; }
      |                ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:890:5: note: candidate: ‘template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)’
  890 |     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:890:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from main.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h:106:23: note:   ‘phanso’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
  106 |       { return *__it1 == *__it2; }
      |    

Aucun commentaire:

Enregistrer un commentaire