[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

patricio26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WeetbixironCommented:
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 :)
0
evilrixSenior Software Engineer (Avast)Commented:
>> So in the object class you should overload the less than operator (<)
It does, but that is not the problem, see my point below about friends

Observations: -
* Your object class is prefixing its functions with the class name when they are defined within the class body. Although some compilers will accept this it is not valid C++.
* You are including object.cpp in main, shouldn't that be object.h or object.hpp?
* You are declaring friend functions that do not exist
* Your non-mutating functions should be const

See your code, modified, below (reordred so it'll compile). I have not testing the logic is correct, I leave that up to you :)
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include "object.cpp"
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() const {
             return myDouble;
             }
      int getMyInt() const {
          return myInt;
      }
      bool operator==(const object& y) const {
           return (myDouble == y.myDouble && myInt == y.myInt);
           }
      bool operator<(const object& y) const {
           if(myDouble == y.myDouble)
                return(myInt < y.myInt);
                else
           return (myDouble < y.myDouble);
           }
      bool operator>(const object& y) const {
           if(myDouble == y.myDouble)
                 return(myInt > y.myInt);
           else
                 return(myDouble > y.myDouble);
           }
      };
 
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;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
patricio26Author Commented:
Sure enough, friend was extraneous, and the second const made it compile.  Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.