Thread: The trouble with polymorphism

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Cool The trouble with polymorphism

    Hello,

    Hope somebody can help. I'm trying to output the following using polymorphism:

    Playing mp3 track: Who let the dogs out? (193 secs)
    Playing wav track: Meowth song (253 secs)
    Playing mp3 track: Thunderball (480 secs)


    I'm sure I'm close, but I'm having trouble. I keep getting 3 errors, all saying:

    "...no overloaded function takes 2 parameters".

    Here is the code:

    Code:
    /* musicLib.h: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class Track
    {
    public:
       Track( string title, int duration ) { }
       virtual void play( ) = 0;
    
    protected:
       int lengthOfTime;
       string name;
    
    };
    
    class Mp3 : public Track
    {
    public:
       Mp3( );
       void play( );
    
    };
    
    class Wav: public Track
    {
    public:
       Wav( );
       void play( );
    
    };
    
    class Playlist
    {
    public:
       Playlist( );
       void add( Track* t ) { gimmeTrack.push_back( t ); }
       void play( );
    
    private:
       typedef std :: vector< Track* > ThemTracks;
       ThemTracks gimmeTrack;
    
    };
    
    --------------------------------------------------------------------------
    
    /* musicImp.cc: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <iostream>
    #include <string>
    #include "musicLib.h"
    
    using namespace std;
    
    void Mp3 :: play( )
    {
      cout << "Playing mp3 track: " << &name << "(" << &lengthOfTime << ")";
    }
    
    void Wav :: play( )
    {
      cout << "Playing wav track: " << &name << "(" << &lengthOfTime << ")";
    }
    
    Playlist :: Playlist( ) { }
    
    void Playlist :: play( )
    {
      vector< Track* > :: iterator i;
      for ( i = gimmeTrack.begin( ); i != gimmeTrack.end( ); ++i )
      {
        ( *i ) ->play( );
      }
    }
    
    -------------------------------------------------------------------------------
    
    /* music.cc: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "musicLib.h"
    
    using namespace std;
    
    int main( )
    {
        Playlist list;
    
        list.add( new Mp3( "Who let the dogs out?", 193 ) );
        list.add( new Wav( "Meowth song", 253 ) );
        list.add( new Mp3( "Thunderball", 480 ) );
    
        list.play();
        
        return 0;
    }
    If somebody knows where I'm going wrong I'd really appreatiate some assistance.

    Cheers,

    Stabbrie

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You're close, but you need to work on calling/implementing your constructors properly:

    Code:
    /* musicLib.h: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class Track
    {
    public:
       Track( string title, int duration ) : name(title),lengthOfTime(duration)  { }
       virtual void play( ) = 0;
    
    protected:
       int lengthOfTime;
       string name;
    
    };
    
    class Mp3 : public Track
    {
    public:
       Mp3( string title,int duration ) : Track(title,duration) {}
       void play( );
    
    };
    
    class Wav: public Track
    {
    public:
       Wav( string title, int duration) : Track(title,duration) {}
       void play( );
    
    };
    
    class Playlist
    {
    public:
       Playlist( );
       void add( Track* t ) { gimmeTrack.push_back( t ); }
       void play( );
    
    private:
       typedef std :: vector< Track* > ThemTracks;
       ThemTracks gimmeTrack;
    
    };
    
    --------------------------------------------------------------------------
    
    /* musicImp.cc: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <iostream>
    #include <string>
    #include "musicLib.h"
    
    using namespace std;
    
    void Mp3 :: play( )
    {
      cout << "Playing mp3 track: " << name << "(" << lengthOfTime << ")" << endl;
    }
    
    void Wav :: play( )
    {
      cout << "Playing wav track: " << name << "(" << lengthOfTime << ")" << endl;
    }
    
    Playlist :: Playlist( ) { }
    
    void Playlist :: play( )
    {
      vector< Track* > :: iterator i;
      for ( i = gimmeTrack.begin( ); i != gimmeTrack.end( ); ++i )
      {
        ( *i ) ->play( );
      }
    }
    
    -------------------------------------------------------------------------------
    
    /* music.cc: developed for wav an mp3 classes tutorial
       Written by Dexter Valentine
       Created 8/3/05 */
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "musicLib.h"
    
    using namespace std;
    
    int main( )
    {
        Playlist list;
    
        list.add( new Mp3( "Who let the dogs out?", 193 ) );
        list.add( new Wav( "Meowth song", 253 ) );
        list.add( new Mp3( "Thunderball", 480 ) );
    
        list.play();
        
        return 0;
    }
    Last edited by hk_mp5kpdw; 04-04-2005 at 07:11 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Cheers, bro.

    Used the new code, will remember that in future, but now I'm getting a couple of errors:

    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/Music.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Music.exe - 2 error(s), 0 warning(s)


    i.e. it compiles fine but wont build.

    Hope I'm not being stupid,

    Cheers for any help.

    Stabbrie

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You started a Win32 non-Console project, which uses WinMain as the startup function, but you didn't supply a WinMain. Either set the project type to Console (makes a console window pop up) or supply a WinMain instead of a main. Since you're using cout, you'll want the former.


    Oh, and I know this is just an exercise, but I should point out that from a design viewpoint, making play() a virtual method of Track doesn't make much sense. The track doesn't play itself, it is played by a player. In other words, there should be a virtual method to obtain the raw audio data (with the tracks supplying track-specific decoders), and a Player class that actually plays the tracks (by obtaining the data, then forwarding it to yet another class hierarchy specifying the output method: Disk File, alsa, esound, arts, ... - I'm somewhat Linux-influenced right now.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You started a Win32 non-Console project, which uses WinMain as the startup function, but you didn't supply a WinMain.
    Your compiler can create at least two types of programs:

    1)A C++ console program where the output goes to a DOS 'console' window.

    2) A 'windows program'. Windows programs are organized completely different than a C++ program. You have to study 'windows programming' to understand the different structure and how to write windows programs.

    So, when you use your compiler to create the files for your program, the first thing your compiler should ask you is what type of program you want to create. You should choose whatever selection has 'console application' in the description.

    At this point, your best course of action is to cut and paste all your code into Notepad, then start a new program, and cut and paste the code into the new program's files. It's impossible to delete a program in most editors. To delete a program, you have to get out of the editor, use your computer to navigate to the files, and then delete the specific folders and files you no longer need.
    Last edited by 7stud; 04-04-2005 at 09:46 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Errm, it's far easier to go to the linker settings of the project and choose "Console" in the Subsystem selection.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Thumbs up

    Cheers Guys,

    Obviously I was being stupid. I must of chose 'Win32 application', instead of 'console' when firing up the project in Visual C++.

    A thousand thanks.

    Stabbrie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Polymorphism
    By GiN0 in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2007, 06:18 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. trouble with polymorphism
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2006, 11:34 PM
  5. Replies: 3
    Last Post: 10-31-2005, 12:05 PM