Thread: Overloading == and < operator

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    48

    Overloading == and < operator

    Ok I have all of my code written, but I am not sure about overloading the == and < operators. I keep getting an error that says no match for operator < in 's1<s3'

    here is the code...I have tried adding
    Code:
    friend Song::operator==(const Song &s)
    but that doesn't work either.
    Code:
    //song.h
    #ifndef SONG_H
    #define SONG_H
    #include <stdlib.h>
    #include <iostream>
    #include <ostream>
    #include <string>
    #include "BobCatPod.h"
    using namespace std;
    
    
    class Song
    {
      
       public:
          Song();
          Song(string , string , int ); // constructor
          void setSize(int s);
          int getSize(); // access or
          void setTitle (string t);
          string getTitle();
          void setArtist (string a);
          string getArtist();
          void addSong (int);
          friend ostream& operator<<(ostream & os, const Song & s);
          
       private:
          string title;    //dynamic allocation
          string artist;
          int size;
    };
    #endif
    Code:
    // poddriver.cpp
    
    //Adapted from Roger Priebe
    //CS2308 11/17/08
    
    #include <stdlib.h>
    #include <iostream>
    #include "BobCatPod.h"
    #include "song.h"
    
    using namespace std;
    
    int main()
    {
        
        Song s1("Frank Sinatra", "My way", 14);
        Song s2("Beatles", "Act Naturally", 5);
        Song s3;
        
        BobCatPod p(256);
        BobCatPod q(512);
        BobCatPod r(25);
    
    
        cout << "pod p" << endl << p << endl;
        cout << "pod q, size 512 exceeds max, should be reset to 256 " << endl
             << q << endl;
        cout << "pod r, size should be 25 " << endl << r << endl;
    
    
        cout << "Song 1" << s1 << endl;
        cout << "Song 2" << s2 << endl;
        cout << "Song 3" << s3 << endl;
    
        s3.setArtist("Buck Owens");
        s3.setTitle("Act Naturally");
        s3.setSize(20);
    
        cout << "Song 3 updated " << s3 << endl;
        cout << "Artist 1 (Frank Sinatra)  " << s1.getArtist() << endl;
        cout << "Title 2 (Act Naturally)  " << s2.getTitle() << endl;
    //    s1.setSize(7);
    //    cout << "Size 1 (7) " << s1.getSize() << endl;
    
    //test relational operators
    
        if (s1 < s3 ) cout << endl << "s1 < s3" << endl;
          else cout << endl << "s1 >= s3" << endl;
        if (s1 == s2) cout << endl << "s1 == s2" << endl;
          else cout << endl << "s1 != s2"  << endl;
        
    // test addnode
        cout << "add song 1, size 14 " << r.addSong(s1) << endl;  
        cout << "   memory left = "  << r.getRemainingMemory() <<endl;
        cout << "add song 2, size 5 " << r.addSong(s2)<< endl; 
        cout << "   memory left = "  << r.getRemainingMemory() << endl;
        cout << "add song 3, size 20 should fail " << r.addSong(s3) <<endl;
        cout << "   memory left = "  << r.getRemainingMemory() << endl;
      
        s3.setSize(2);
        cout << "Size 3 (2) " << s3.getSize() << endl;
        cout << "add song 3, size 2 should succeed " << r.addSong(s3) 
             << "   memory left = "  << r.getRemainingMemory() << endl;
    
    
    // test output
        cout << r << endl;
    
    //test delete
    
        r.removeSong(s1);
        cout << r << endl;
        r.removeSong(s3);
        cout << r << endl;
    
        cout << "Total Memory = "<< r.getTotalMemory() << endl;
    
    
    
    
    
        return 0;
    }
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looking at all your accessor functions, you probably can declare them as free functions:
    Code:
    bool operator==(const Song& lhs, const Song& rhs);
    bool operator<(const Song& lhs, const Song& rhs);
    Oh, and please remove that using directive from your header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    Ok here is everything. why won't it work? I am getting so frustrated with this I feel like I have a puzzle with all the edge pieces missing.

    Code:
    //song.h
    #ifndef SONG_H
    #define SONG_H
    #include <stdlib.h>
    #include <iostream>
    #include <ostream>
    #include <string>
    #include "BobCatPod.h"
    
    
    
    class Song
    {
      
       public:
          Song();
          Song(string , string , int ); // constructor
          void setSize(int s);
          int getSize(); // access or
          void setTitle (string t);
          string getTitle();
          void setArtist (string a);
          string getArtist();
          void addSong (int);
          friend ostream& operator<<(ostream & os, const Song & s);
          bool operator==(const Song& lhs, const Song& rhs);
          bool operator<(const Song& lhs, const Song& rhs);
       private:
          string title;    //dynamic allocation
          string artist;
          int size;
    };
    #endif
    Code:
    // file song.cpp
    #include "song.h"
    #include "BobCatPod.h"
    
     ostream & operator << ( ostream & os, Song & s)
     {
          os << s.getTitle();
          os  << s.getArtist();
          os << s.getSize();
          return os;
     }
    //constructor
    Song::Song ()
    {
        title=" ";
        artist=" ";
        size = 0;
    }
    Song::Song(string a, string t, int s) // constructor
    {
        size = s;
        title = t;
        
        artist = a ;
        
    }
    //accessor for name
    string Song :: getTitle()
    {
          return title;
    }
    //mutator
    void Song :: setTitle (string t)
    {
         title = t;
         
    }
    string Song :: getArtist()
    {
          return artist;
    }
    //mutator
    void Song :: setArtist (string a)
    {
         artist = a;
         
    }
    void Song::setSize(int s)
    {
        size = s;
    }
    int Song::getSize()
    {
        return size;
    }
    Code:
    //BobCatPod.h
    
    //Specification files for the SongList class
    
    #ifndef BOBCATPOD_H
    #define BOBCATPOD_H
    #include "song.h"
    
    
    
    class BobCatPod
    {
          
          private:
          struct SongNode
          {
                 Song s;                  //value in this node
                 SongNode *next;
          
                             
          };
          
          SongNode *head;
          static const int MAX_MEMORY = 256;
          
          
          public:
                 int totalMemory;
                 int remainingMemory;
                 
                 
                 //Constructor
                 BobCatPod();
    
                     
                 //Linked list operations
                 BobCatPod::BobCatPod(int i);
                 bool isFull ( ) const;
                 int addSong (Song s);
                 int removeSong (Song s);
                 void showSongList ();
                 int getTotalMemory();
                 int getRemainingMemory();
                 friend ostream & operator<<(ostream & os, const BobCatPod & i);
                 
                 
                 //Destructor
                 ~BobCatPod();
    
    };
    #endif
    Code:
    //BobCatPod.cpp
    #include "song.h"
    #include "BobCatPod.h"
    
    
    
    BobCatPod::BobCatPod()
    {
         head = NULL;
         
    }
    
    bool BobCatPod::isFull ( ) const
    {
         if (totalMemory > MAX_MEMORY)
         {
          
                    return true;
         }
    }
    
    BobCatPod::BobCatPod(int i)  
    {
        
        if( i <= MAX_MEMORY)
            remainingMemory = i;    
        else
            remainingMemory = MAX_MEMORY;
    }
    
    int BobCatPod::addSong(Song t)
    {
          if (isFull ())
         {
                    return 0;
         }
         SongNode *newSong = new SongNode;     //a new node
         
        
         
         // Allocate a new node and stor num there.
         
         newSong->s = t; 
         head = newSong;
         newSong->next = NULL;
         totalMemory++;
        
         return 1;
    }
    
    
    int BobCatPod::removeSong( Song t )
    {
         if (isFull ())
         {
                    totalMemory--;
                    return 1;
         }
         else     
         return 1;
    }
     
    void BobCatPod::showSongList( )
    {
         SongNode *n;
         cout<<endl;
     
         for( n = head ; n != NULL ; n = n->next )
            cout<<endl<<n->s;
    }
    
    int BobCatPod::getTotalMemory()
    {
        Song t;
        totalMemory = t.getSize();
        return totalMemory;
    }
    
    int BobCatPod::getRemainingMemory()
    {
        
        remainingMemory = MAX_MEMORY - totalMemory;
        return remainingMemory;
    }
    Code:
    // poddriver.cpp
    
    //Adapted from Roger Priebe
    //CS2308 11/17/08
    
    #include <stdlib.h>
    #include <iostream>
    #include "BobCatPod.h"
    #include "song.h"
    
    using namespace std;
    
    int main()
    {
        
        Song s1("Frank Sinatra", "My way", 14);
        Song s2("Beatles", "Act Naturally", 5);
        Song s3;
        
        BobCatPod p(256);
        BobCatPod q(512);
        BobCatPod r(25);
    
    
        cout << "pod p" << endl << p << endl;
        cout << "pod q, size 512 exceeds max, should be reset to 256 " << endl
             << q << endl;
        cout << "pod r, size should be 25 " << endl << r << endl;
    
    
        cout << "Song 1" << s1 << endl;
        cout << "Song 2" << s2 << endl;
        cout << "Song 3" << s3 << endl;
    
        s3.setArtist("Buck Owens");
        s3.setTitle("Act Naturally");
        s3.setSize(20);
    
        cout << "Song 3 updated " << s3 << endl;
        cout << "Artist 1 (Frank Sinatra)  " << s1.getArtist() << endl;
        cout << "Title 2 (Act Naturally)  " << s2.getTitle() << endl;
    //    s1.setSize(7);
    //    cout << "Size 1 (7) " << s1.getSize() << endl;
    
    //test relational operators
    
        if (s1 < s3 ) cout << endl << "s1 < s3" << endl;
          else cout << endl << "s1 >= s3" << endl;
        if (s1 == s2) cout << endl << "s1 == s2" << endl;
          else cout << endl << "s1 != s2"  << endl;
        
    // test addnode
        cout << "add song 1, size 14 " << r.addSong(s1) << endl;  
        cout << "   memory left = "  << r.getRemainingMemory() <<endl;
        cout << "add song 2, size 5 " << r.addSong(s2)<< endl; 
        cout << "   memory left = "  << r.getRemainingMemory() << endl;
        cout << "add song 3, size 20 should fail " << r.addSong(s3) <<endl;
        cout << "   memory left = "  << r.getRemainingMemory() << endl;
      
        s3.setSize(2);
        cout << "Size 3 (2) " << s3.getSize() << endl;
        cout << "add song 3, size 2 should succeed " << r.addSong(s3) 
             << "   memory left = "  << r.getRemainingMemory() << endl;
    
    
    // test output
        cout << r << endl;
    
    //test delete
    
        r.removeSong(s1);
        cout << r << endl;
        r.removeSong(s3);
        cout << r << endl;
    
        cout << "Total Memory = "<< r.getTotalMemory() << endl;
    
    
    
    
    
        return 0;
    }
    I feel like i am just plugging holes in a sinking ship and I am running out of fingers.

    Thanks for all the help I appreciate it

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, free functions, so they are not part of the class.

    However, all your get member functions should be declared const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 10:33 AM
  2. C++ Overloading < > == for use with char * or string
    By neolyn in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2004, 03:37 PM
  3. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM
  4. Opinion on Overloading < and > For My Number Class
    By golfinguy4 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2003, 05:33 PM
  5. Overloading < or > operators
    By mouse163 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2003, 08:32 PM