Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Vektor problem z fukcją wirtualną

Ostatnio zmodyfikowano 2015-09-21 19:01
Autor Wiadomość
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.

C/C++
class Inventory
{
public:
    vector < Item > items;
    //virtual void add_item(string item);
    void add_item( Item & item );
    void deleteItem( int i );
    void showInventory();
   
   
};
//wycinek z pliku Inventory.cpp
void Inventory::showInventory()
{
    if( 0 < items.size() )
    {
        for( unsigned short i = 0; i < items.size(); i++ )
        {
            items.at( i ).showItem();
            cout << endl;
        }
    }
   
}

C/C++
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();
};
// funkcje
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;
}
P-137717
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.

P-137734
Acarin1995
Temat założony przez niniejszego użytkownika
» 2015-09-20 21:05:20

C/C++
// Wcześniej dodawałem elementy w taki sposób o było bardzo korzystne a teraz mam dodawać każdy z osobna? Tak: items.push_back(std::make_unique<Armour>("ertert", "4er4te", 5, 2));

void Inventory::addItem( Item & item )
{
    items.push_back( item );
}

//Przedmiot:

Armour koszula( "Koszula", "Prosta lniana koszula", 1, Armour::TORSO );
P-137746
Monika90
» 2015-09-20 22:23:49
To sobie napisz szablon
C/C++
class Inventory
{
    //...
    template < class Item >
    void addItem( const Item & );
   
    //...
};

Definicja w tym samym pliku w którym masz definicję klasy Inventory
C/C++
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:
C/C++
items.at( i )->showItem();
P-137749
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;
C/C++
//Inventory.hpp
class Inventory
{
public:
    template < class Item >
    vector < unique_ptr < Item >> items;
    //vector <Item> items;
    //void add_item(Item &item);
    //void deleteItem(int i);
    void addItem( const Item & );
    void showInventory();
   
   
};
//Inventory.cpp
#include "Inventory.hpp"

/*
void Inventory::deleteItem(int i)
{
    items.erase(items.begin()+i);
}
*/
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;
        }
    }
   
}
P-137752
Monika90
» 2015-09-20 23:53:28
To funkcja addItem miała być szablonem, a items zadeklaruj tak jak Ci napisałam wcześniej
C/C++
std::vector < std::unique_ptr < Item >> items;
P-137754
Acarin1995
Temat założony przez niniejszego użytkownika
» 2015-09-21 17:45:16
C/C++
//Inventory.hpp
class Inventory
{
public:
    vector < unique_ptr < Item >> items;
    template < class Item >
    void addItem( const Item & )
    void showInventory();
   
   
};
//Inventory.cpp
#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
P-137772
Monika90
» 2015-09-21 18:01:23
No to zrób tak
C/C++
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.
P-137776
« 1 » 2
  Strona 1 z 2 Następna strona