Greetings All,

I have one class I'll call main class and it creates and initializes all of the classes so that they are not re-created again. What I simply want to do is to have in the creation of my other classes pointers to the classes they need to operate.

I get errors when I try, and I have looked in various forums. The question is, how is the proper way to do this, if it can/should be done. I don't want to have to pass each one for each function that needs it. Thank you in advance for any help.

here is the class that creates the new classes

EcoAreaTeam*		m_pEcoAreaTeam = new  EcoAreaTeam();
LifePattern*		m_pLifePattern = new  LifePattern();
NeedTeam*			m_pNeedTeam  = new  NeedTeam();
SceneFrameTeam*		m_pSceneFrameTeam  = new  SceneFrameTeam(m_pEcoAreaTeam, m_pStatisticCalculation,
															m_pSymptomManager, m_pTrioTeam);
StatisticCalculation*   m_pStatisticCalculation  = new  StatisticCalculation();
SymptomManager*		m_pSymptomManager  = new  SymptomManager(); 
TrioTeam*			m_pTrioTeam  = new  TrioTeam();

here is a class that i trying to use to access pointers to these classes .cpp file

SceneFrameTeam::SceneFrameTeam(EcoAreaTeam* eco, StatisticCalculation* stat,
					  SymptomManager* sym, TrioTeam* trio):
						m_pEcoAreaTeam(eco),
						m_pStatisticCalculation(stat),
						m_pSymptomManager(sym),
						m_pTrioTeam(trio){}

here is the .h file creation information:

class   EcoAreaTeam;
class	SceneFrameBase;
class   StatisticCalculation;
class   SymptomManager;
class   TrioTeam;

//these are the pointers to the classes
EcoAreaTeam*			m_pEcoAreaTeam; 
StatisticCalculation*	m_pStatisticCalculation;
SymptomManager*			m_pSymptomManager; 
TrioTeam*				m_pTrioTeam;

here is the constructor info:

SceneFrameTeam(EcoAreaTeam* eco,
             StatisticCalculation* stat,
             SymptomManager* sym,
             TrioTeam* trio);

this is the error message:

error C2065: 'eco' : undeclared identifier

and

error C3867: 'SceneFrameTeam::EcoAreaTeam': function call missing argument list; use '&SceneFrameTeam::EcoAreaTeam' to create a pointer to member

the thing is I was looking at an example from Matt Buckland's AI book and he did not seem to need the &

I guess the question is, is there a simple way to do this?

Peace,

Marcia

Your code is spaced all over the place. Mixed spaces and tabs?

If your basic question is how to initialize reference members of a class, you do it like this:

class Class { /*...*/ };

class Class2 {
    Class& m_class_ref;
    //...
public:
    Class2(Class& class_ref);
    //...
};

Class2::Class2(Class& class_ref) : m_class_ref(class_ref) {
    //...
}

@ nucleon I have tried this and got an error...will re-try and re post. I know your code is correct...I am simply missing something. Thank you for your reply

You don't seem to have posted enough code for anyone to tell but are you using: m_pEcoAreaTeam = new EcoAreaTeam(); and then the class does not have m_pEcoAreaTeam and you have a scope issue??

If the class has m_pEcoAreaTeam check the spelling. And why the use a global m_pEcoAreaTeam or was that just to illistrate ?

@ stuXYZ, yes you are correct it seems to be a scope issue. I have not been clear. What it is is that

(a) the first Mainclass creates all the classes. Two of the classes that it creates are: m_pArtClass = new ArtClass();

it also creates m_pSchoolClass = new SchoolClass();

(b) Now the thing is that in the SchoolClass, there are some functions where it needs to call the ArtClass(). For example:
m_pArtClass->Paint();

(c) So what I want to avoid is having to create a new ArtClass in the SchoolClass for it to access. I simply wanted the MainClass to create the SchoolClass() with a pointer to the ArtClass like this:

(d)
SchoolClass* m_pSchoolClass = new SchoolClass(m _pArtClass);

As you see the ArtClass() would be in the constructor for the SchoolClass to simply use, it would not need to create a *new* ArtClass and could simply use the one that was passed in

Thank you. I hope this is not too fuzzy

So you mean something like this?

class ArtClass {
};

class School {
    ArtClass* m_artclass;
public:
    School(ArtClass* artclass) { m_artclass = artclass; }
};

class Main {
    School*   m_school;
    ArtClass* m_artclass;
public:
    Main() {
        m_artclass = new ArtClass();
        m_school = new School(m_artclass);
    }
    ~Main() {
        delete m_school;
        delete m_artclass;
    }
};

@nucleon...you have inspired me to help others on this forum..inspired me to become a better coder..inspired me to stick with daniweb--it has really proven itself as a great c++ forum, with folks like you. Your answer was very thorough and your time very appreciated:)

Your code snippet is exactly what I have been trying to do and the compiler complains--although I have sine done a work around. I am committed to look at each dot(.) and everything you have done to see what I must have overlooked.

Thank you...what you wrote is exactly what I though I was doing--but I must have missed something...May you be continually blessed

I can't believe it it worked!!!!!!!!!!! :) :) :)

You have no idea how this saves me in the future from having to pass in classes to every function that would need it :(

@ nucleon you are an angel...the problem was that I was not defining "artclass" correctly, which was why the compiler kept on saying it was not defined--who would have thought the compiler hadn't made a mistake(smile)...

blessings to you nucleon..you helped my day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.