“I would like to be able to call the color method with player1, but the method is not defined in the player1 class; it is defined in the player2 class, specifically in the characteristic class. However, it seems that calling a method from class A to class B is impossible, and the book does not mention it.”
“I have commented the lines in the code where I think my problem lies …”
Here is the error I am encountering:
Exception raised: read access violation. this was nullptr.”
//Heritage.h
#pragma once
#ifndef HeritageHPP
#define HeritageHPP
#include <iostream>
#include <string>
class Caracteristique;
class Personnages
{
public:
Personnages(int vie, int age, std::string nom, std::string prenom /*Caracteristique cara*/) : m_age(age), m_vie(vie), m_nom(nom), m_prenom(prenom)/* m_caracteristique(cara) */ {} // Constructeur du personnage
void Perso1(std::string nom, std::string prenom);
void Perso2(std::string nom, std::string prenom);
void couleur(std::string couleur);
void affiche() const { std::cout << m_nom <<" " << m_prenom; }
private:
int m_age, m_vie;
std::string m_nom, m_prenom;
Caracteristique * test; // are this good ?
};
class Caracteristique : public Personnages
{
public:
Caracteristique(int vie, int age, std::string nom, std::string prenom, std::string origine = "Europeen") : Personnages(vie, age, nom, prenom), /*Caracteristique cara*/ m_origine(origine) {}
void couleur(std::string couleur);
void taille(int taille);
void poids(int poids);
//void affiche() const { std::cout << m_couleur << std::endl; }
//Personnages instancePersonnages;
private:
std::string m_origine;
std::string m_couleur;
int m_taille;
int m_poids;
Personnages* test;
};
//Heritage.cpp
//Pour la classe de bases.
//Méthode de perso1
#include "Heritage.h"
void Personnages::Perso1(std::string nom, std::string prenom)
{
m_nom = nom;
m_prenom = prenom;
}
//Méthodes de perso2
void Personnages::Perso2(std::string nom, std::string prenom)
{
m_nom = nom;
m_prenom = prenom;
}
//My probleme is here also
void Personnages::couleur(std::string couleur)
{
test->couleur(couleur);
}
/*
void Personnages::couleur(std::string couleur)
{
m_caracteristique.couleur(couleur);
}
*/
//Pour la classe Caractéristique.
/*
void Caracteristique::couleur(std::string couleur)
{
m_couleur = couleur;
}
*/
void Caracteristique::taille(int taille)
{
m_taille = taille;
}
void Caracteristique::poids(int poids)
{
m_poids = poids;
}
//My Probleme is here
void Caracteristique::couleur(std::string couleur)
{
test->couleur(couleur);
}
//main.cpp
#include <iostream>
#include "Heritage.h"
int main()
{
//Creation des objets.
Personnages Joueur1 (50, 20, "molikor","David");
Caracteristique Joueur2(20, 50," "," "," ");
//Apppel des méthodes pour les Personnages
//Perso1
Joueur1.Perso1("Molikor ", "Jean n"); //Nom prenom du perso1
Joueur2.Perso2("Tilor ", "Patrick"); //Nom prenom du perso2
//Perso2
//Joueur1.couleur("Chatain"); //Couleur de cheveux du joueur1 chatin.
//Joueur2.couleur("Brun"); //Couleur de cheveux du joueur2 brun.
Joueur1.affiche();
Joueur2.couleur("rose");
Joueur2.affiche();
Joueur1.couleur("Jaune"); // Here is the probleme....
Joueur1.affiche();
}
Snake-Eagel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.