Cześć, mam problem z poniższym kodem (fragmenty):
Mam klase abstrkcyjną "Potion"
Potion.h:
#pragma once
#include <iostream>
#include <string>
class Potion
{
std::string name;
public:
Potion(const std::string n = "");
Potion(const Potion & p);
virtual ~Potion();
void setName(std::string n) { name = n; }
std::string getName()const { return name; }
virtual void printPotionType()const = 0;
bool operator==(const Potion & p)const;
friend std::ostream & operator<<(std::ostream & os, const Potion & p);
};
i klasę pochodną "Heatlh potion"
HealthPotion.h
#pragma once
#include "Potion.h"
class HealthPotion : public Potion
{
int power;
public:
HealthPotion(std::string name, int p);
~HealthPotion();
int getPower()const { return power; }
void printPotionType()const;
};
definicja metody printPotionType() const z pliku HealthPotion.cpp, w pliku Potion.cpp takowej nie ma.
void HealthPotion::printPotionType()const
{
std::cout << "Health potion, power = " << power << std::endl;
}
dodatkowo jest jeszcze klasa "Player", która posiada std::vector na obiekty klasy Potion, wygląda to tak:
#pragma once
#include <iostream>
#include <vector>
#include "Item.h"
#include "Potion.h"
class Player
{
...
std::vector<Potion> potions;
public:
...
void addPotion(const Potion & p);
void printPotions();
void Drink(const Potion & p);
Potion & getPotion(unsigned n);//zwraca referencje do elementu wektora potions o indeksie podanym jako argument
...
};
oraz definicje tych metod:
void Player::addPotion(const Potion & p)
{
potions.push_back(p);
}
void Player::printPotions()
{
cout << "POTIONS: " << endl;
for(auto & p : potions)
{
cout << p << endl;
}
}
void Player::Drink(const Potion & p)
{
for (auto it = potions.begin(); it != potions.end(); it++)
{
if (*it == p)
{
cout << p.getName() << " used" << endl;
potions.erase(it);
break;
}
}
}
Potion & Player::getPotion(unsigned n)
{
if (n <= potions.capacity())
return potions[n];
else
{
cout << "Player doesn't have so many potions" << endl;
}
}
przy próbie kompilacji wyświetla mi się takie coś:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): error C2259: 'Potion': cannot instantiate abstract class
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: due to following members:
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: 'void Potion::printPotionType(void) const': is abstract
1> c:\users\dudyc\desktop\programowanie obiektowe\c++\lista5\potion.h(15): note: see declaration of 'Potion::printPotionType'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,Potion&>(_Objty *,Potion &)' being compiled
1> with
1> [
1> _Ty=Potion,
1> _Objty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,Potion&>(_Objty *,Potion &)' being compiled
1> with
1> [
1> _Ty=Potion,
1> _Objty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,Potion&>(std::allocator<_Ty> &,_Objty *,Potion &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<Potion>,
1> _Ty=Potion,
1> _Objty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,Potion&>(std::allocator<_Ty> &,_Objty *,Potion &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<Potion>,
1> _Ty=Potion,
1> _Objty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1284): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,Potion&>(_Ty *,Potion &)' being compiled
1> with
1> [
1> _Ty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1283): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,Potion&>(_Ty *,Potion &)' being compiled
1> with
1> [
1> _Ty=Potion
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1276): note: while compiling class template member function 'void std::vector<Potion,std::allocator<_Ty>>::push_back(const Potion &)'
1> with
1> [
1> _Ty=Potion
1> ]
1> c:\users\dudyc\desktop\programowanie obiektowe\c++\lista5\player.cpp(30): note: see reference to function template instantiation 'void std::vector<Potion,std::allocator<_Ty>>::push_back(const Potion &)' being compiled
1> with
1> [
1> _Ty=Potion
1> ]
1> c:\users\dudyc\desktop\programowanie obiektowe\c++\lista5\player.h(14): note: see reference to class template instantiation 'std::vector<Potion,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=Potion
1> ]
Przypuszczam, że może to mieć związek z deklaracją vectora "potions" ale nie mam pojęcia z czym dokladnie.
Czy byłby ktoś w stanie wyjaśnić mi co jest w tym kodzie nie tak? Szukam już po internecie prawie dwie godziny i nie mogę znaleźć rozwiązania.