-2

I'm doing an assignment with virtual classes, I need to implement a pure virtual method which later will be implemented in an inherited class.

I solved a problem before with pure virtual methods which worked flawlessly, solving by myself the error i receive now ( error C2259: 'Playlist': cannot instantiate abstract class) - by implementing a method in an inherited class.

class Record
{
    char* artist;
    char* titlu;
    int durata;

public:
    Record()
    {
        artist = new char[50];
        titlu = new char[50];
        durata = 0;
    }
    void setArtist(char *s)
    {
        strcpy(artist, s);
    }
    char* getArtist()
    {
        return artist;
    }
    void setTitlu(char* s)
    {
        strcpy(titlu, s);
    }
    char* getTitlu()
    {
        return titlu;
    }
    void setDurata(int n)
    {
        int durata = n;

    }
    int getDurata()
    {
        return durata;
    }


};
class Playlist
{
    Record* p; /// "Record" is another class
public:
    Playlist(int n)
    {
        p = new Record[n];
    }
    virtual void sortare(int n) = 0;


};
class PlaylistImplementation :public Playlist
{
public:
    void sortare(int n) 
    {
        if (n == 1)
        {
            cout << "1";
        }
        else if (n == 2)
        {
            cout << "2";
        }
        else
            cout << "3";
    }

};

Here is the main():

int main()
{
    int n;
    cout << "Number of tracks ?";
    cin >> n;
    Playlist x(n);  /// I use VS 2019 and "x" has a red tilde underneath, 
                         ///  also error is at this line.
    cin.ignore();
    cin.get();
    return 0;
}

I expect to instantiate an object from class Playlist.

tgarmp
  • 35
  • 9

2 Answers2

1

You can't instantiate Playlist directly because it is abstract, so in main, instead of:

Playlist x(n);

you need

PlaylistImplementation x(n);

And you need a constructor in PlaylistImplementation like so:

PlaylistImplementation (int n) : PlayList (n) { }
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
1

Member 'p' of type Record in class Playlist is implicitly declared private. For access you need to add public member functions (e.g. to store data to a record).

Mathias Schmid
  • 431
  • 4
  • 7