Problem with classes

This is a discussion on Problem with classes within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to access a class from an other class but it keeps giving me error about not recognising ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    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.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    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()

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    I don't think the problem is there, I always use that header guard.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    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

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 08:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 01:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 08:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21