Acarin1995 Temat założony przez niniejszego użytkownika |
Vektor problem z fukcją wirtualną » 2015-09-20 16:30:44 Witam mam problem z funkcją wirtualną która jest odpowiedzialna za wyświetlanie zmiennych danej klasy. Program się kompiluje ale wyświetla zmienne zawarte tylko w metodzie klasy item pomijając zmienne z klas pochodnych Fragmenty kodu poniżej. class Inventory { public: vector < Item > items; void add_item( Item & item ); void deleteItem( int i ); void showInventory(); };
void Inventory::showInventory() { if( 0 < items.size() ) { for( unsigned short i = 0; i < items.size(); i++ ) { items.at( i ).showItem(); cout << endl; } } }
class Item { public: string name; string description; virtual void showItem(); };
class Armour : public Item { public: int defense; enum Slot { HEAD, TORSO, LEGS, FEET }; Slot slot; Armour( string name, string description, int defense, Armour::Slot slot ); virtual void showItem(); };
class Weapon : public Item { public: int attackMin; int attackMax; enum WeaponType { PRIMARY, SECONDARY, DUALWIELD, TWOHAND, NOWEAPON }; WeaponType weaponType; Weapon( string name, string description, int attackMin, int attackMax, Weapon::WeaponType weaponType ); virtual void showItem(); };
void Item::showItem() { cout << name << endl; cout << description << endl; } void Weapon::showItem() { cout << name << endl; cout << description << endl; cout << "Obrazenia " << attackMin << " - " << attackMax << endl; }
void Armour::showItem() { cout << name << endl; cout << description << endl; cout << "Pancerz " << defense << endl; }
|
|
Monika90 |
» 2015-09-20 19:26:35 Wektor wskaźników (inteligentnych): std::vector<std::unique_ptr<Item>> items;
Elementy się dodaje do niego w ten sposób: items.push_back(std::make_unique<Armour>("ertert", "4er4te", 5, 2));
Klasa Item musi mieć wirtualny destruktor.
|
|
Acarin1995 Temat założony przez niniejszego użytkownika |
» 2015-09-20 21:05:20
void Inventory::addItem( Item & item ) { items.push_back( item ); }
Armour koszula( "Koszula", "Prosta lniana koszula", 1, Armour::TORSO );
|
|
Monika90 |
» 2015-09-20 22:23:49 To sobie napisz szablon class Inventory { template < class Item > void addItem( const Item & ); };
Definicja w tym samym pliku w którym masz definicję klasy Inventory template < class Item > void Inventory::addItem( const Item & item ) { items.push_back( std::make_unique < Item >( item ) ); }
I będziesz mógł dodawać itemy po staremu. Tylko pamiętaj że teraz items jest wektorem wskaźników, więc trzeba użyć strzałki: items.at( i )->showItem();
|
|
Acarin1995 Temat założony przez niniejszego użytkownika |
» 2015-09-20 23:09:26 Inventory.hpp:10:36: error: data member 'items' cannot be a member template vector<unique_ptr<Item>> items;
class Inventory { public: template < class Item > vector < unique_ptr < Item >> items; void addItem( const Item & ); void showInventory(); };
#include "Inventory.hpp"
void Inventory::addItem( const Item & item ) { items.push_back( std::make_unique < Item >( item ) ); } void Inventory::showInventory() { if( 0 < items.size() ) { for( unsigned short i = 0; i < items.size(); i++ ) { items[ i ]->showItem(); cout << endl; } } }
|
|
Monika90 |
» 2015-09-20 23:53:28 To funkcja addItem miała być szablonem, a items zadeklaruj tak jak Ci napisałam wcześniej std::vector < std::unique_ptr < Item >> items;
|
|
Acarin1995 Temat założony przez niniejszego użytkownika |
» 2015-09-21 17:45:16 class Inventory { public: vector < unique_ptr < Item >> items; template < class Item > void addItem( const Item & ) void showInventory(); };
#include "Inventory.hpp"
template < class Item > void Inventory::addItem( const Item & item ) { items.push_back( make_unique < Item >( item ) ); }
Log: I:\Programowanie\Nevermore 2015 marzec\Nevermore Console\Inventory.cpp: In member function 'void Inventory::addItem(const Item&)': I:\Programowanie\Nevermore 2015 marzec\Nevermore Console\Inventory.cpp:15:22: error: 'make_unique' was not declared in this scope items.push_back( make_unique < Item >( item ) ); ^ I:\Programowanie\Nevermore 2015 marzec\Nevermore Console\Inventory.cpp:15:41: error: expected primary-expression before '>' token items.push_back( make_unique < Item >( item ) );
Czy na pewno konieczne jest wykorzystanie inteligentnych wskaźników? Same z nimi problemy. :D |
|
Monika90 |
» 2015-09-21 18:01:23 No to zrób tak items.push_back( unique_ptr < Item >( new Item( item ) ) );
Najwyraźniej masz stary kompilator albo nie włączyłeś C++14 i dlatego nie ma make_unique. |
|
« 1 » 2 |