From: Lukas Jiriste Date: Fri, 25 Oct 2024 14:36:55 +0000 (+0200) Subject: Fix issues preventing compilation X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=8ad01d1871ee1cc369b07b094cfc8dbd41a2b468;p=42%2FCPP_Module_04 Fix issues preventing compilation These include mostly missing includes, missing declarations, missing Class:: namespaces before function definitions and typos. A circular dependency between header files of AMateria and ICharacter needed to be solved. --- diff --git a/ex03/AMateria.cpp b/ex03/AMateria.cpp index 831d4c3..2156958 100644 --- a/ex03/AMateria.cpp +++ b/ex03/AMateria.cpp @@ -1,6 +1,7 @@ #include "AMateria.h" #include "ICharacter.h" #include +#include AMateria::AMateria() { @@ -17,6 +18,9 @@ AMateria::~AMateria() AMateria &AMateria::operator=(const AMateria &other) { + if (this == &other) + return (*this); + return (*this); } AMateria::AMateria(const std::string &type) @@ -24,12 +28,17 @@ AMateria::AMateria(const std::string &type) { } -const std::string &getType() const +const std::string &AMateria::getType() const { return (m_type); } -void use(ICharacter &target); +void AMateria::use(ICharacter &target) { std::cout << "* AMATERIA AFFECTS " << target.getName() << " *\n"; } + +bool AMateria::operator==(const std::string &type) +{ + return (m_type == type); +} diff --git a/ex03/AMateria.h b/ex03/AMateria.h index b4c8073..f20b143 100644 --- a/ex03/AMateria.h +++ b/ex03/AMateria.h @@ -4,6 +4,8 @@ # include "ICharacter.h" # include +class ICharacter; + class AMateria { private: @@ -21,6 +23,8 @@ class AMateria virtual AMateria *clone() const = 0; virtual void use(ICharacter &target); + + bool operator==(const std::string &type); }; #endif // AMATERIA_H diff --git a/ex03/Character.cpp b/ex03/Character.cpp index f776520..64c4526 100644 --- a/ex03/Character.cpp +++ b/ex03/Character.cpp @@ -11,7 +11,7 @@ Character::Character(const std::string &name) : m_name(name) { for (size_t i(0); i < INVENTORY_SIZE; ++i) - inventory[i] = NULL; + m_inventory[i] = NULL; } Character::Character(const Character &other) @@ -19,21 +19,24 @@ Character::Character(const Character &other) { for (size_t i(0); i < INVENTORY_SIZE; ++i) { - if (other.inventor[i] == NULL) - inventory[i] = NULL; + if (other.m_inventory[i] == NULL) + m_inventory[i] = NULL; else - inventory[i] = other.inventory[i]->clone(); + m_inventory[i] = other.m_inventory[i]->clone(); } } Character::~Character() { for (size_t i(0); i < INVENTORY_SIZE; ++i) - delete inventory[i]; + delete m_inventory[i]; } Character &Character::operator=(const Character &other) { + if (this == &other) + return (*this); + return (*this); } void Character::equip(AMateria *materia) @@ -41,25 +44,25 @@ void Character::equip(AMateria *materia) for (size_t i(0); i < INVENTORY_SIZE; ++i) if (m_inventory[i] == NULL) { - m_inventory[i] = materia + m_inventory[i] = materia; return ; } } void Character::unequip(int idx) { - if (idx < 0 || idx >= INVENTORY_SIZE) + if (idx < 0 || (size_t)idx >= INVENTORY_SIZE) return ; m_inventory[idx] = NULL; } void Character::use(int idx, ICharacter &target) { - if (idx < 0 || idx >= INVENTORY_SIZE) + if (idx < 0 || (size_t)idx >= INVENTORY_SIZE) return ; - if (m_inventory[i] == NULL) + if (m_inventory[idx] == NULL) return ; - m_inventory[i]->use(target); + m_inventory[idx]->use(target); } const std::string &Character::getName() const diff --git a/ex03/Character.h b/ex03/Character.h index 0428f6d..bbbd733 100644 --- a/ex03/Character.h +++ b/ex03/Character.h @@ -19,9 +19,10 @@ class Character : public ICharacter AMateria *m_inventory[INVENTORY_SIZE]; public: - Character(std::string name); + Character(const std::string &name); ~Character(); + const std::string &getName() const; void equip(AMateria *materia); void unequip(int idx); void use(int idx, ICharacter &target); diff --git a/ex03/Cure.cpp b/ex03/Cure.cpp index 56eaff2..3a8b745 100644 --- a/ex03/Cure.cpp +++ b/ex03/Cure.cpp @@ -17,14 +17,17 @@ Cure::~Cure() Cure &Cure::operator=(const Cure &other) { + if (this == &other) + return (*this); + return (*this); } -AMateria *clone() const +AMateria *Cure::clone() const { - return (new Ice(*this)); + return (new Cure(*this)); } -void use(ICharacter& target) +void Cure::use(ICharacter& target) { - std::cout << "* heals " << target.getName << "'s wounds *\n"; + std::cout << "* heals " << target.getName() << "'s wounds *\n"; } diff --git a/ex03/Cure.h b/ex03/Cure.h index 2b9ee9d..775a15d 100644 --- a/ex03/Cure.h +++ b/ex03/Cure.h @@ -1,6 +1,9 @@ #ifndef CURE_H # define CURE_H +# include "AMateria.h" +# include "ICharacter.h" + class Cure : public AMateria { private: @@ -10,6 +13,9 @@ class Cure : public AMateria Cure(); Cure(const Cure &other); ~Cure(); + + AMateria *clone() const; + void use(ICharacter &target); }; #endif // CURE_H diff --git a/ex03/ICharacter.h b/ex03/ICharacter.h index 91e015e..fb353cc 100644 --- a/ex03/ICharacter.h +++ b/ex03/ICharacter.h @@ -1,6 +1,11 @@ #ifndef ICHARACTER_H # define ICHARACTER_H +# include "AMateria.h" +# include + +class AMateria; + class ICharacter { public: diff --git a/ex03/IMateriaSource.h b/ex03/IMateriaSource.h index fbd16af..515d9f7 100644 --- a/ex03/IMateriaSource.h +++ b/ex03/IMateriaSource.h @@ -6,7 +6,7 @@ class IMateriaSource public: virtual ~IMateriaSource() {} - virtual void leanMateria(AMateria *) = 0; + virtual void learnMateria(AMateria *) = 0; virtual AMateria *createMateria(const std::string &type) = 0; }; diff --git a/ex03/Ice.cpp b/ex03/Ice.cpp index 2e8a357..98a0d11 100644 --- a/ex03/Ice.cpp +++ b/ex03/Ice.cpp @@ -7,7 +7,7 @@ Ice::Ice() } Ice::Ice(const Ice &other) - : AMateria(other); + : AMateria(other) { } @@ -17,14 +17,17 @@ Ice::~Ice() Ice &Ice::operator=(const Ice &other) { + if (this == &other) + return (*this); + return (*this); } -AMateria *clone() const +AMateria *Ice::clone() const { return (new Ice(*this)); } -void use(ICharacter& target) +void Ice::use(ICharacter& target) { - std::cout << "* shoots an ice bolt at " << target.getName << " *\n"; + std::cout << "* shoots an ice bolt at " << target.getName() << " *\n"; } diff --git a/ex03/Ice.h b/ex03/Ice.h index c69a36e..f8d1130 100644 --- a/ex03/Ice.h +++ b/ex03/Ice.h @@ -1,6 +1,9 @@ #ifndef ICE_H # define ICE_H +# include "AMateria.h" +# include "ICharacter.h" + class Ice : public AMateria { private: @@ -10,6 +13,9 @@ class Ice : public AMateria Ice(); Ice(const Ice &other); ~Ice(); + + AMateria *clone() const; + void use(ICharacter &target); }; #endif // ICE_H diff --git a/ex03/MateriaSource.cpp b/ex03/MateriaSource.cpp index c97a13f..a84d6fc 100644 --- a/ex03/MateriaSource.cpp +++ b/ex03/MateriaSource.cpp @@ -1,4 +1,5 @@ #include "MateriaSource.h" +#include "AMateria.h" MateriaSource::MateriaSource() : m_num_known(0) @@ -20,7 +21,7 @@ MateriaSource::~MateriaSource() void MateriaSource::deleteKnown() { - for (; m_num_know > 0;) + for (; m_num_known > 0;) delete m_known_materia[--m_num_known]; } @@ -29,6 +30,7 @@ MateriaSource &MateriaSource::operator=(const MateriaSource &other) deleteKnown(); for (; m_num_known < other.m_num_known; ++m_num_known) m_known_materia[m_num_known] = other.m_known_materia[m_num_known]->clone(); + return (*this); } void MateriaSource::learnMateria(AMateria *materia) @@ -42,7 +44,7 @@ void MateriaSource::learnMateria(AMateria *materia) AMateria *MateriaSource::createMateria(const std::string &type) { for (size_t i(0); i < m_num_known; ++i) - if (m_known_materia[i] == type) + if (*m_known_materia[i] == type) return (m_known_materia[i]->clone()); return (NULL); } diff --git a/ex03/MateriaSource.h b/ex03/MateriaSource.h index 72fedf1..0c6ca2a 100644 --- a/ex03/MateriaSource.h +++ b/ex03/MateriaSource.h @@ -4,10 +4,11 @@ # include "AMateria.h" # include "IMateriaSource.h" -class MateriaSource : IMateriaSource +class MateriaSource : public IMateriaSource { private: MateriaSource &operator=(const MateriaSource &other); + void deleteKnown(); static const size_t max_known = 4;