Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Baloogan

AGGGGG... more class parent/child problems....

This topic is 5930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was helped before on these forms and it worked but when I finished that project and moved on to another the pointer thing did not work!!!
  
class CHouse;


class CCat
{
CHouse *House;
void think();
}

class CHouse
{
vector<CCat> Cats;
}


  
Is this teh right way?????

Share this post


Link to post
Share on other sites
Advertisement
I am not sure what you are trying to do, but I made a snippet for you to play with. It involves a basic parent/child system.


#include <stdio.h>

int g_iPeople;

const int UNDIES = 0;
const int PANTS = (1<<0);
const int SHIRT = (1<<1);

class CPerson;
class CPants;
class CShirt;

class CPants
{ public:

CPants ( CPerson *pParent );
void DoSomething( );

private:

CPerson *m_pParent;
};

class CPerson
{ public:
CPants *m_pPants;
CShirt *m_pShirt;

void AddPants( )
{
printf("Adding pants... \n");
m_pPants = new CPants(this);
};
CPerson ( )
{
printf("New person. \n");
g_iPeople++;
printf("Total people: %d \n", g_iPeople);
};

int m_iClothes;

};

CPants::CPants ( CPerson *pParent ) : m_pParent(pParent)
{
m_pParent->m_iClothes |= PANTS;

printf("Now wearing pants. \n");
}

void CPants::DoSomething( )
{
printf("The pants are now glowing. \n");
}

int main ( int argc, char argv )
{
CPerson Taciturn;
Taciturn.AddPants( );
Taciturn.m_pPants->DoSomething();

if ( Taciturn.m_iClothes & PANTS && ! (Taciturn.m_iClothes & SHIRT) )
{
printf("Taciturn is only wearing pants! \n");
}

return 1;
}


[edit]
Trying to make the code tag work.
[/edit]

[edited by - taciturn on September 15, 2002 11:25:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Baloogan
the pointer thing did not work!!!




what didn''t work about "the pointer thing"? did your program compile? did it crash when you executed it? try to be more specific. btw, all of your class members are private if you do not specify otherwise which may cause problems. on that same point you know there is a beginners forum right?

Share this post


Link to post
Share on other sites
quote:

class CHouse;
class CCat
{
CHouse *House;
void think();
}

class CHouse
{
vector Cats;
}



Try this:

class CHouse;
class CCat
{
CHouse *House;
void think();
};

class CHouse
{
vector Cats;
};

Hope this helps!


-G|aD-

Share this post


Link to post
Share on other sites
quote:
Original post by Baloogan
i get errors of use of undefined type: CHouse...

Where? If you use the House pointer inside an inline method (the think method perhaps) before CHouse is defined you will get this error. Make it out-of-line instead. There is an article about this here on GameDev, but I dunno where. Use the search...

  
// Cat.h

#ifndef _CAT_H_

#define _CAT_H_

class CHouse;
class CCat
{
public:
CHouse *House;
void think();
}
#endif


  
// House.h

#ifndef _HOUSE_H_

#define _HOUSE_H_

#include <Cat.h>

class CHouse
{
public:
vector<CCat> Cats;
}
#endif


  
// Cat.cpp

#include <Cat.h>

#include <House.h>

void CCat::think()
{
House->Cats.push_back(this);
}


[edited by - dalleboy on September 16, 2002 5:20:51 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!