Link to home
Start Free TrialLog in
Avatar of patricio26
patricio26

asked on

[Linkier error] undefined reference to 'operator<(object const&, object const&)'

Hi ladies/gents,
  These two files compile just fine alone, and I can do a global compile if I comment out the sort() algorithm used in main, but when I try to do a normal compile, I get the error message, 5 times:
[Linker error] undefined reference to 'operator<(object const&, object const&)'

Any ideas on why that is?  My overloading of the comparison operators seems just fine to me.  Thanks in advance!
-Patrick
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "object.cpp"
using namespace std;
 
 
int main(int argc, char *argv[])
{
    
    
    object array[100000];
    
    
    for(int i = 0; i < 100000; i++){
            int myInt = rand();
            double myDouble = double(rand()/10);
            object o(myDouble, myInt);
            array[i] = o;            
            }              
            
    int clo = clock();         
        sort(array, array+100000);
    cout << (clock()-clo) << endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
 
 
 
 
 
 
 
 
 
using namespace std;
class object{
      
      private:
              int myInt;
              double myDouble;            
     public:           
            friend bool operator==(const object& x, const object& y);
            friend bool operator<(const object& x, const object& y);
            friend bool operator>(const object& x, const object& y);
             object(){                      
                      myDouble = 0.0;
                      myInt = 0;
                     
                      }
             object(double d, int i){
                      myDouble = d;
                      myInt = i;
                      }
      double getMyDouble(){
             return myDouble;
             }
      int getMyInt(){
          return myInt;
      }
      bool object::operator==(const object& y){
           return (myDouble == y.myDouble && myInt == y.myInt);
           }
      bool object::operator<(const object& y){
           if(myDouble == y.myDouble)
                return(myInt < y.myInt);
                else
           return (myDouble < y.myDouble);
           }
      bool object::operator>(const object& y){
           if(myDouble == y.myDouble)
                 return(myInt > y.myInt);
           else
                 return(myDouble > y.myDouble);
           }
      };

Open in new window

Avatar of Weetbixiron
Weetbixiron

Hi,

The sort function uses the less than operator to decide how to sort an array. So in the object class you should overload the less than operator (<). For details on operator overloading read this: http://www.cplusplus.com/doc/tutorial/classes2/

Good luck :)
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patricio26

ASKER

Sure enough, friend was extraneous, and the second const made it compile.  Thank you!