cannot sort structure !

This is a discussion on cannot sort structure ! within the C++ Programming forums, part of the General Programming Boards category; i am unable to compile this code..can any 1 help Code: #include <cstdio> #include <cmath> #include <algorithm> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    cannot sort structure !

    i am unable to compile this code..can any 1 help
    Code:
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    struct fraction
    {
    	int num,den;
    	
    	fraction(){} 
    	fraction(int a,int b) {insert(a,b);}
    	
    	void insert(int a,int b)
    	{
    		int g=__gcd(a,b);
    		if(g!=0) {a/=g;b/=g;} // reducing to lowest terms !
    		if(b<0)  {a=-a;b=-b;} // changing the sign !
    		num=a;den=b;
    	}
    	bool operator <(fraction A)
    	{
    		return (num*A.den-den*A.num<0);
    	}
    	bool operator >(fraction A)
    	{
    		return (num*A.den-den*A.num>0);
    	}
    	bool operator ==(fraction A)
    	{
    		return (num*A.den-den*A.num==0);
    	}
    };
    
    int main()
    {
    	fraction a[100];
    	sort(a,a+100);
    	return 0;
    }
    Thank you !

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you were unable to compile the code, then presumably there was some message involved.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    Done

    Code:
    bool operator <(const fraction &A) const
    changing to the above works .but donno why ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you can't call non-const functions on constant or temporary objects. (Temporary would be the key here, I'm guessing.)

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It might depend on the implementation. You can look it up from the error, where it happens in particular. With GCC (first) error occurs in this function:

    Code:
      template<typename _Tp>
        inline const _Tp&
        __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
    As you can notice your object is passed by const reference to a function that uses your operator<.

    In general, it is absolutely necessary that comparing objects doesn't change them (imagine objects were changing while you are sorting them!), and C++ uses the const keyword to enforce that this requirement is met.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21