-
Problem with classes
Hi,
I'm trying to access a class from an other class but it keeps giving me error about not recognising the class. For example, if I try to access ClEntreprise in the class ClVisite (sorry about french word) it tell's me it doesn't exist.
Code:
#if !defined(VISITE_H_INCLUS)
#define VISITE_H_INCLUS
#include <iostream>
#include <string>
#include "ClVendeur.h"
#include "ClEntreprise.h"
using namespace std;
class ClVisite
{
public :
ClVisite(int p_date, int p_noContrat, bool p_estExistante)
: m_date(p_date), m_noContrat(p_noContrat), m_estExistante(p_estExistante)
{}
int Date() const { return m_date; }
int NoContrat() const { return m_noContrat; }
bool EstExistante() const { return m_noContrat; }
private :
int m_date;
ClVendeur* m_pVendeur;
ClEntreprise* m_pEntreprise;
int m_noContrat;
bool m_estExistante;
};
#endif
Code:
#if !defined(ENTREPRISE_H_INCLUS)
#define ENTREPRISE_H_INCLUS
#include <iostream>
#include <string>
#include <list>
#include "ClVisite.h"
using namespace std;
typedef list<ClVisite> listeVisite;
class ClEntreprise
{
public :
ClEntreprise(string p_nom, int p_noTéléphone, listeVisite p_listeVisite)
: m_nom(p_nom), m_noTéléphone(p_noTéléphone), m_listeVisite(p_listeVisite)
{}
string Nom() const { return m_nom; }
int NoTelephone() const { return m_noTéléphone; }
listeVisite ListeVisite() const { return m_listeVisite; }
private :
string m_nom;
int m_noTéléphone;
listeVisite m_listeVisite;
};
#endif
I have never had this problem before in other work I have done, help would be appreciated.
-
try using
Code:
#ifndef VISITE_H_INCLUS
#define VISITE_H_INCLUS
There may be nothing wrong with what you use but I've never heard of header guards using if !deinfed()
-
I don't think the problem is there, I always use that header guard.
-
I found the problem, I had too many include
Code:
#if !defined(VISITE_H_INCLUS)
#define VISITE_H_INCLUS
#include <iostream>
#include <string>
using namespace std;
class ClVendeur;
class ClEntreprise;
class ClVisite
{
public :
ClVisite(int p_date, int p_noContrat, bool p_estExistante)
: m_date(p_date), m_noContrat(p_noContrat), m_estExistante(p_estExistante)
{}
int Date() const { return m_date; }
int NoContrat() const { return m_noContrat; }
bool EstExistante() const { return m_noContrat; }
private :
int m_date;
ClVendeur* m_pVendeur;
ClEntreprise* m_pEntreprise;
int m_noContrat;
bool m_estExistante;
};
#endif
-
class ClVendeur;
class ClEntreprise;
These are called forward declarations. They can be used to tell the compiler "trust me, a class/struct by this name will be declared and defined later time so don't complain". They are appropriate to use when two classes are "dependent" on each other because you can only declare one class at a time so only one class can come first.