[C++] Przydział obiektu abstrakcyjnego.

0

Cześć. Jestem nowy na forum. Zaczynam programowanie w c++. Uczę się z książki "Język c++ szkoła programowania" i wykonuje zawarte w niej zadania. Czy mógłby mi ktoś mądrzejszy ode mnie pomóc w rozwiązaniu mojego problemu? A mianowicie podczas kompilacji programu wyskakuje mi błąd: "|22|error: cannot allocate an object of abstract type 'PokerPlayer'|" Oto pliki programu:


//person.h

#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

class Person
{
private:
    string fullname;
protected:
    virtual void Dane() const;
    virtual void Get();
public:
    Person() : fullname("NIE PODANO") {}
    Person(const string & str) : fullname(str) {}
    virtual ~Person() = 0;
    virtual void Show() const = 0;
    virtual void Set() = 0;
};

class Gunslinger : virtual public Person
{
private:
    double czas;
    int naciecia;
protected:
    void Dane() const;
    void Get();
public:
    Gunslinger() : Person(), naciecia(0), czas(0) {}
    Gunslinger(const string & str, int n, double t) : Person(str), naciecia(n), czas(t) {}
    Gunslinger(const Person & Per, int n, double t) : Person(Per), naciecia(n), czas(t) {}
    ~Gunslinger() {}
    double Draw() const;
    void Show() const;
    void Set();
};

class PokerPlayer : virtual public Person
{
private:
    int LastCard;
protected:
    void Dane() const;
    void Get();
public:
    PokerPlayer() : Person() { Draw(); }
    PokerPlayer(const string & str) : Person(str) { Draw(); }
    PokerPlayer(const Person & Per) : Person(Per) { Draw(); }
    ~PokerPlayer() {}
    int Draw();
    void Show();
    void Set();
};

class BadDude : public Gunslinger, public PokerPlayer
{
protected:
    void Dane() const;
    void Get();
public:
    BadDude() : PokerPlayer(), Gunslinger() {}
    BadDude(const string & str, int nac, double t) : Person(str), Gunslinger(str, nac, t), PokerPlayer(str) {}
    BadDude(const Person & Per, int nac, double t) : Person(Per), Gunslinger(Per, nac, t), PokerPlayer(Per) {}
    BadDude(const Gunslinger & guns) : Person(guns), Gunslinger(guns), PokerPlayer(guns) {}
    BadDude(const PokerPlayer & Pok, int nac, double t) : Person(Pok), Gunslinger(Pok, nac, t), PokerPlayer(Pok) {}
    double Gdraw() const;
    int Cdraw();
    void Show() const;
    void Set();
};
#endif // PERSON_H_INCLUDED
//definicje.cpp

#include "person.h"
//person
void Person::Dane() const
{
    cout <<"Imie i nazwisko: "<<fullname;
}
void Person::Get()
{
    cout <<"Podaj imie i nazwisko: ";
    getline(cin, fullname);
}
Person::~Person() {}

//rewolwer
void Gunslinger::Dane() const
{
    cout <<"\nIlosc naciec na rewolwerze: "<<naciecia;
}
void Gunslinger::Get()
{
    cout <<"Podaj liczbe naciec na rewolwerze: ";
    cin >> naciecia;
}
void Gunslinger::Set()
{
    Person::Get();
    Get();
}
void Gunslinger::Show() const
{
    cout <<"Rewolwerowiec: \n";
    Person::Dane();
    Dane();
}
double Gunslinger::Draw() const
{
    return czas;
}

//poker
void PokerPlayer::Dane() const
{
    cout <<"\nOstatnia karta to: "<<LastCard;
}
void PokerPlayer::Get()
{
    Draw();
}
int PokerPlayer::Draw()
{
    srand(time(0));
    LastCard = rand() % 52 + 1;
    return LastCard;
}
void PokerPlayer::Show()
{
    cout <<"Pokerzysta:\n";
    Person::Dane();
    Dane();
}
void PokerPlayer::Set()
{
    Person::Get();
    Draw();
}

    //BadDude
void BadDude::Dane() const
{
    Person::Dane();
    PokerPlayer::Dane();
}
void BadDude::Get()
{
    Person::Get();
    cout << endl;
    PokerPlayer::Dane();
    Gunslinger::Dane();
}
void BadDude::Show() const
{
    cout <<"Zly ksiaze:\n";
    Person::Dane();
    PokerPlayer::Dane();
    Gunslinger::Dane();
}
void BadDude::Set()
{
    Person::Get();
    PokerPlayer::Get();
    Gunslinger::Get();
}
int BadDude::Cdraw()
{
    return PokerPlayer::Draw();
}
double BadDude::Gdraw() const
{
    return Gunslinger::Draw();
}
//main.cpp

#include "person.h"

int main()
{
    cout << "Czy to dzia³a? - PokerPlayer WTF??????????????\n\n " << endl;
    const int SIZE = 5;
    Person * Tab[SIZE];
    int ct;
    for (ct = 0; ct <SIZE; ct++)
    {
        int choice;
        cout << "Kim chcesz byc???? :D\n"
                "1: pokerzysta, 2: rewolwerowiec, 3: Zly gosc!, 4: wyjscie\n";
        while (!(cin >> choice) && (choice == 1 || choice == 2 || choice == 3 || choice == 4))
        {
            cout <<"Wpisz 1 lub 2 lub 3 lub 4!";
        }
        if (choice == 4)
            break;
        switch(choice)
        {
           case 1 : Tab[ct] = new PokerPlayer; //Tutaj jest blad
                break;
            case 2 : Tab[ct] = new Gunslinger;
                break;
            case 3 : Tab[ct] = new BadDude;
                break;
        }
        cin.get();
        Tab[ct]->Set();
    }
    cout <<"\nLista bohaterow:\n";
    int i;
    for (i = 0; i < ct;i++)
    {
        cout<<endl;
        Tab[i]->Show();
    }
    for (i = 0; i < ct; i ++)
        delete Tab[i];

    cin.get();
    cin.get();
    return 0;
}

Kompilator (Code::Blocks) wskazuje błąd w pliku main.cpp linia 22.
Proszę o pomoc. Z góry dziękuje!

3

W klasie bazowej masz:

virtual void Show() const = 0;

W klasie pochodnej masz:

void Show();

To nie jest ta sama funkcja. To jest inna funkcja, bo nie ma const. Skutkuje to tym, że czysto wirtualna funkcja z klasy bazowej nie jest zaimplementowana i klasa jest abstrakcyjna.

0

Dziękuje bardzo za pomoc. Wczoraj siedziałem nad tym 2h i nie mogłem znaleźć tak drobnego błędu. :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1