Potomek klasy wirtualnej - cannot declarate variable object to be of abstract type class error

0

ok wiec mam trzy pliki
zawartosc hedera ekwipunek

#ifndef EKWIPUNEK_H
#define EKWIPUNEK_H
#include <iostream>
#include <iostream>
#include <cmath>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstdio>
#include<conio.h>

using namespace std;



class item
{
public:
    virtual void set_attack(int a)=0;
    virtual void set_defensive(int a)=0;
    virtual void set_value (int a)=0;
    virtual void set_name(string a)=0;
    virtual void set_description(string a)=0;
    virtual void set_ID(int a)=0;
    virtual void set_weight(int a)=0;
    virtual int get_attack()=0;
    virtual int get_defensive()=0;
    virtual int get_value ()=0;
    virtual string get_name()=0;
    virtual string get_description()=0;
    virtual int get_ID()=0;
    virtual int get_weight()=0;
    virtual void print_all()=0;
};

class sword
:public item
{
    int sword_attack;
    int value;
    int weight;
    int ID;
    string name;
    string description;
    public:

    sword(string name="TEST_SWORD_NAME",int sword_attack=0,int weight=0,int ID=0,string description="TEST_SWORD_DESCRIPTION");
     void set_attack(int a);
    void set_value (int a);
     void set_name(string a);
    void set_description(string a);
    void set_ID(int a);
     void set_weight(int a);
     int get_attack();
     int get_value ();
    string get_name();
string get_description();
     int get_ID();
     int get_weight();
     void print_all();
};

class bow
:public item
{
    int bow_attack;
    int value;
    int weight;
    int ID;
    string name;
    string description;
    bow(string name="TEST_BOW_NAME",int bow_attack=0,int weight=0,int ID=0,string description="TEST_BOW_DESCRIPTION");
     void set_attack(int a);
    void set_value (int a);
    void set_name(string a);
    void set_description(string a);
     void set_ID(int a);
    void set_weight(int a);
     int get_attack();
    int get_value ();
    string get_name();
    string get_description();
     int get_ID();
     int get_weight();
    void print_all();

};


class backpack
{
    item *slot[100];
    item *pointer;
    int max_weight;
    int current_weight;
    backpack();

    void item_add(item &stuff);
    void item_check(item &stuff);
};
#endif // EKWIPUNEK_H

zawartosc cpp ekwipunek

#include "ekwipunek.h"
#include <iostream>
#include <iostream>
#include <cmath>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstdio>
#include<conio.h>


sword::sword(string name,int sword_attack,int weight,int ID,string description)
{
    this->sword_attack=(sword_attack);
    this->value=(value);
    this->weight=(weight);
    this->ID=(ID);
    this->name=(name);
    this->description=(description);
}
void sword::set_attack(int a)
{
    sword_attack = a;
}

void sword::set_value(int a)
{
    value = a;
}
void sword::set_weight(int a)
{
    weight =a ;
}
void sword::set_ID(int a)
{
    ID = a;
}
void sword::set_name(string a)
{
    name = a;
}
void sword::set_description(string a)
{
    description = a;
}
int sword::get_attack()
{
    return sword_attack;
}

int sword::get_value ()
{
    return value;
}
string sword::get_name()
{
    return name;
}
string sword::get_description()
{
    return description;
}
int sword::get_ID()
{
    return ID;
}
int sword::get_weight()
{
    return weight;
}
void sword::print_all()
{
    cout<<endl;
    cout<<name;
    cout<<endl;
    cout<<"---------------------------------------------\n";
    cout<<sword_attack<<endl;
    cout<<weight<<endl;
    cout<<value<<endl;
    cout<<description<<endl;
    cout<<"---------------------------------------------\n";
}





//------------------------------------------------------------------------------------------------------------




bow::bow(string name,int bow_attack,int weight,int ID,string description)
{
    this->bow_attack=(bow_attack);
    this->value=(value);
    this->weight=(weight);
    this->ID=(ID);
    this->name=(name);
    this->description=(description);
}
void bow::set_attack(int a)
{
    bow_attack = a;
}

void bow::set_value(int a)
{
    value = a;
}
void bow::set_weight(int a)
{
    weight =a ;
}
void bow::set_ID(int a)
{
    ID = a;
}
void bow::set_name(string a)
{
    name = a;
}
void bow::set_description(string a)
{
    description = a;
}
int bow::get_attack()
{
    return bow_attack;
}

int bow::get_value ()
{
    return value;
}
string bow::get_name()
{
    return name;
}
string bow::get_description()
{
    return description;
}
int bow::get_ID()
{
    return ID;
}
int bow::get_weight()
{
    return weight;
}
void bow::print_all()
{
    cout<<endl;
    cout<<name;
    cout<<endl;
    cout<<"---------------------------------------------\n";
    cout<<bow_attack<<endl;
    cout<<weight<<endl;
    cout<<value<<endl;
    cout<<description<<endl;
    cout<<"---------------------------------------------\n";



}







//-------------------------------------------------------------------------------------------

backpack::backpack()
{
    max_weight=100;
    current_weight=0;


}


void backpack::item_add(item &stuff)

{
item *wskaznik;
wskaznik = &stuff;

    for(int i=0; i<100; i++)
    {


        if(slot[i]==NULL)
            i=i;
        else
        {
            if(wskaznik->get_weight()+current_weight>max_weight)
            {


                cout<<"nie ok";
                break;
            }
                else
                {
                    slot[i]= &stuff;
                    current_weight=current_weight+wskaznik->get_weight();
                    cout<<"ok";
                    break;
                }

        }

    }
}

i zawartosc maina

#include <iostream>
#include "ekwipunek.h"

using namespace std;

 main()
{

sword mieczyk;
backpack krystian;
krystian.item_Add(&mieczyk);
}

Błąd wywala w linijce sword mieczyk; w mainie

Prosiłbym także o sprawdzenie czy moja funkcja w backpack cpp item_add ma prawo działać;

1

Nie chce mi się szukać która to, ale błąd oznacza że w klasie sword nie zaimplementowałeś któreś z tych czysto wirtualnych metod z klasy item.

0

Na pewno źle wygląda

using namespace std;

w pliku nagłówkowym. Zła praktyka.

0
  1. krystian.item_Add(&mieczyk);
    W C++ nazwy item_add i item_Add to nie to samo.
    Skoro item_add przyjmuje referencję, to nie podawaj wskaźnika.
  2. W pliku nagłówkowym ogranicz liczbę include'ów do minimum, wystarczy Ci tylko #include <string>, resztę dołączasz w plikach źródłowych. Usuń zdublowany #inlcude <iostream>.
  3. Uwagi do metody item_add:
    Taki warunek
if(slot[i]==NULL)
        i=i;
    else

daje odwrotny efekt do zamierzonego, tzn. wstawia wskaźnik w pierwsze zajęte miejsce zamiast w pierwsze wolne. A po Ci i=i nawet nie pytam.

W tym przypadku

cout<<"nie ok";
break;

metoda powinna zwrócić jakąś informację, czy udało się włożyć przedmiot do plecaka, np. true lub false:
bool backpack::item_add(item &stuff)

0

Nie widzę zbytniego sensu w "wirtualizacji" metod dla id, name czy desc. IMHO te pola powinny być składowymi klasy bazowej (Item) i nie powinno być możliwości ich zmiany po utworzeniu instancji. Żeby ładnie wyglądało to upakowałbym to w jakąś strukturę Czyli coś w stylu:


struct ItemInitializer {
  std::string name;
  std::string desc;
  int id;
  ItemInitializer( int id, const std::string& name = "", const std::string& desc = "" ) : id( id ), name( name ), desc( desc ) { }
};
class Item {
public:
  const std::string name;
  const std::string desc;
  const int id;
public:
  Item( const ItemInitializer& item_props ) : id( item_props.id ), name( item_props.name ), desc( item_props.desc ) { }
  virtual ~Item() { }
  virtual bool IsValid() const = 0;
};

Idąc dalej, kolejną w hierarchii byłaby klasa powiedzmy InventoryItem która albo byłaby bazową dla wszelkiego rodzaju przedmiotów, albo można by zastosować system komponentów.

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