Home > Back-end >  How can I fix my C code ? (no match for operators)
How can I fix my C code ? (no match for operators)

Time:11-10

How can I fix my C code ? (no match for operators) I got an error : no match for "operators-". What can be the problem and how can I solve it? Can anybody help me to fix it ?

error: no match for 'operator-' (operand types are 'std::set::iterator' {aka 'std::_Rb_tree_const_iterator'} and 'std::set::iterator' {aka 'std::_Rb_tree_const_iterator'})|

#include <iostream>
#include <set>
using namespace std;

int main()
{
    int O;
    int N;
    int M;

    cin >> O >> N >> M;

    int tanarsorszama[O];
    int tantargysorszama[O];
    int nap [O];
    int ora [O];

    for(int i=0; i<O; i  )
    {
        cin >> tanarsorszama[i] >> tantargysorszama[i] >> nap[i] >> ora[i];
    }

    int dblyukas[N]={0};
    set<int>orai; 

    for(int i=0; i<N; i  )
    {
        for(int k=1; k<6; k  )
        {
           orai.clear();  

            for(int j=0; j<9; j  )
            {
                if(tanarsorszama[i]!=0 && ora!=0 && nap[k]!=0)
                {
                    orai.insert(ora[j]);   
                }
            }
            if (orai.size() > 1) 
            {
              dblyukas[i]  = orai.end() - orai.begin()  1 - orai.size();  // There is the error
            }
         }
     }
return 0;
}

CodePudding user response:

std::set doesn't have random-access iterators. That is why you are getting the error.

To access the first and last elements of a set, use orai.begin() and std::prev(orai.end()). These return iterators, which will have to be de-referenced with operator*. So, correcting what I think you are intending to do, leads to the following:

            if (orai.size() > 1) 
            {
              dblyukas[i]  = *std::prev(orai.end()) - *orai.begin()  1 - orai.size();  // There is the error
            }
  • Related