Problem with my code, I think it’s due to circular inclusion

“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();

}

New contributor

Snake-Eagel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật