-1

My c++ code written for solving a spoj problem is not getting compiled.It compiles very weint on my pc but not on spoj or ideone.com. I am having trouble in understanding the error that it shows.Please help!! http://ideone.com/3Wce5t

  #include<iostream>
  #include<cstdio> 
  #include<algorithm>
  #include<vector>

   using namespace std;

    struct point{
    int xx;int yy;int zz;

    bool const operator < (point& b)
    const {
    return xx < b.xx;
    }
    };

    bool myfun(const point& a,const point& b)
    {
    return a.yy < b.yy;
    }





    const int N = 1000001;
    int tree[N],L[N],R[N];
    int next;
    vector <point> v;


    void build(int ID,int l,int r)
    {
    tree[ID] = 0;
    int m = (l+r)>>1;

    if(l<r)
    {
    L[ID] = next++;
    build(L[ID],l,m);
    R[ID] = next++;
    build(R[ID],m+1,r);
    }
    else
        {L[ID] = -1;R[ID] = -1;}
    }

    void update(int ID,int id,int l,int r,int loc)  //new ID and old id
    {
    int m = (l+r)>>1;

    if(l==r)
        {
        tree[ID] = 1;
        return; 
        }

    L[ID] = L[id];
    R[ID] = R[id];

    if(l<=loc && loc<=m)
        {L[ID] = next++;
        update(L[ID],L[id],l,m,loc);
        }
    if((m+1)<=loc && loc<=r)
        {R[ID] = next++;
        update(R[ID],R[id],m+1,r,loc);  
        }
    tree[ID] = tree[L[ID]] + tree[R[ID]];

    }

    int get(int id,int ID,int k,int l,int r)
    {
    if(l==r) return v[l].xx;

    int mid = (l+r)>>1;

    //cout<<tree[L[ID]] - tree[L[id]]<<' '<<k<<' '<<l<<' '<<r<<endl;

    if(tree[L[ID]]-tree[L[id]] >= k) 
            return get(L[id],L[ID],k,l,mid);
    else    
            return get(R[id],R[ID],k-(tree[L[ID]]-tree[L[id]]),mid+1,r);
    }


    int main()
    {
    int n,i,j,m;

    cin>>n>>m;

    v.resize(n);

    for(i=0;i<n;i++)
        {cin>>v[i].xx;
        v[i].yy = i;    
        }   

    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
        v[i].zz = i;

    sort(v.begin(),v.end(),myfun);

    next = 2;

    build(1,0,n-1);

    vector <int> ind(n+1);
    ind[0] = 1;

    for(i=1;i<=n;i++)
        {
        ind[i] = next++;    
        update(ind[i],ind[i-1],0,n-1,v[i-1].zz);
            //ID and then location
        }//we set original v[i]'s position in sorted as 1

    int a,b,c;

    sort(v.begin(),v.end());

    for(i=0;i<m;i++)
        {
        cin>>a>>b>>c;
        a--;
        cout<<get(ind[a],ind[b],c,0,n-1)<<endl; 
        }

    }

The error I am getting is::

/usr/include/c++/4.3/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = point]':
        /usr/include/c++/4.3/bits/stl_algo.h:1919:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point*, std::vector<point, std::aintocator<point> > >, _Size = int]'
        /usr/include/c++/4.3/bits/stl_algo.h:4783:   instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<point*, std::vector<point, std::aintocator<point> > >]'
        prog.cpp:105:   instantiated from here
        /usr/include/c++/4.3/bits/stl_algo.h:93: error: no match for 'operator<' in '__a < __b'
        prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
        /usr/include/c++/4.3/bits/stl_algo.h:94: error: no match for 'operator<' in '__b < __c'
        prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
        /usr/include/c++/4.3/bits/stl_algo.h:96: error: no match for 'operator<' in '__a < __c'
        prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
        /usr/include/c++/4.3/bits/stl_algo.h:100: error: no match for 'operator<' in '__a < __c'
        prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
        /usr/include/c++/4.3/bits/stl_algo.h:102: error: no match for 'operator<' in '__b < __c'
        prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
Shubham Ugare
  • 103
  • 1
  • 6
  • 2
    When posting questions about build errors, please include the actual errors, in full, complete and unedited (including any informational notes the compiler might output) in the actual body of the question. – Some programmer dude Jun 22 '16 at 08:51
  • ok I will do that. – Shubham Ugare Jun 22 '16 at 08:52
  • 1
    Better post an [mcve], but `bits/stdc++.h` is not a standard header file. – juanchopanza Jun 22 '16 at 08:53
  • 3
    As for the code you show, using undecipherable type-aliases and using macros that redefine common symbols are maybe good for you, right now, but not for others that are supposed to read and understand the code. And with "others" I include you in a couple of months time when you had time to forget about these things and then try to reread the code. Things like that is the road to [The Daily WTF](http://thedailywtf.com). – Some programmer dude Jun 22 '16 at 08:54
  • 2
    [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – πάντα ῥεῖ Jun 22 '16 at 08:54
  • 3
    SPOJ is not code golf. Get rid of `LL` and that macro nonsense!! – Angew is no longer proud of SO Jun 22 '16 at 08:57
  • Sorry for all the mistakes.I use macros to save time in competitive coding.I know it makes code very unreadable and error prone. I have made all the changes. – Shubham Ugare Jun 22 '16 at 09:12
  • 2
    How does making your code unreadable and error prone "save time"? – Lightness Races in Orbit Jun 22 '16 at 10:19
  • _"It compiles very weint on my pc"_ Assuming this statement is intended to mean that the code works on your PC, I cannot see how that would be the case, unless you're using MSVS I suppose (which allows binding temporaries to refs-to-non-`const`) – Lightness Races in Orbit Jun 22 '16 at 10:20

2 Answers2

0

As stated in the error, the compiler can't find a correct comparison operator because it should be defined as

bool const operator < (point b)

and not

bool const operator < (point& b)

EDIT : as cited in the commentary,

bool const operator < ( point const & b)

may be the better way

Guiroux
  • 531
  • 4
  • 11
-1

Move overloading operator outside of struct:

struct point
{
    int xx;
    int yy;
    int zz;
};

bool const operator < (const point &a, const point &b) const
{
    return xx < b.xx;
}
MaciekGrynda
  • 583
  • 2
  • 13