Classes Inside Classes

class CharStatsClass
{
public:
      CharStatsClass(int SStats);
      ~CharStatsClass();
      void AddStrength();
      void AddMagic();
      void AddStamina();
      void AddCon();
      void AddInt();
      int GetStrength();
      int GetMagic();
      int GetStamina();
      int GetCon();
      int GetInt();
private:
      int RStats;
      int PStrength;
      int PMagic;
      int PStamina;
      int PCon;
      int PInt;
};

class CharClass
{
public:
      CharClass (char* Name);
      ~CharClass();
      CharStatsClass CharStats;
      char* GetName();
private:
      char* PName;
};

CharStatsClass::CharStatsClass(int SStats)
{
      RStats=SStats;
};
CharStatsClass::~CharStatsClass (){}


CharClass::CharClass(char* Name)
{
      PName = Name;
}

CharClass::~CharClass(){}


Okay, my problem here is ::

CharClass::CharClass(char* Name)
{ ---------->error C2512: 'CharStatsClass' : no appropriate default constructor available

It works fine if I remove CharStatsClass, yet erros on CharClass.
I was told I would be better of with Types, but right now, I am
just trying to play around with classes, not actually writing a
program. Any help would be great.
LVL 8
List244Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ambienceCommented:
Well there is no default constructor available, since you have provided you own for CharStatClass (and that one takes a parameter). Two ways to resolve this

1- Call the constructor directly as

CharClass::CharClass(char* Name): CharStats(0) // <---
{
     PName = Name;
}

2- Provide a default constructor

class CharStatsClass
{
public:
     CharStatsClass() { /* Rstats = 0; */ }
...
}



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambienceCommented:
oops

>> Provide a default constructor

should've been " Provide a parameter-less constructor ".
0
okidachiCommented:
Since you have declared your  CharStatsClass class with the constructor

CharStatsClass(int SStats); ==> means you override the default constructor

If you don't declare any constructor for your class, C++ will implicitly declare a default constructor which one that has no parameters for you. In your case, you declare a new one and override the default.

In the CharClass class, you declare

     CharStatsClass CharStats; ==> C++ will lookup your constructors in the CharStatsClass class and find the one with has no parameters, so it fails to do that  because you don't have any constructors with no parameters. The point is that you don't have any constructor, C++ implicitly gives you one constructor with no parameters, if you declare at least one, so that is the constructor you have, C++ will not do anything to your class.
0
AxterCommented:
Just to add to previous comments:

If you have a member object which has required arguments for the constructor, you need to either use the initialize list, or create the object dynamicly.

//Initialize list method
CharClass::CharClass(char* Name)
: CharStats(0) // This is the initialize list
{
     PName = Name;
}

More over, you can initialize your member variables there as well.
//Example
CharClass::CharClass(char* Name)
: CharStats(0), PName(Name)
{
   //Body of constructor
}

It's more efficient to initialize your member variables via initialize list then it is in the constructor

Some times you don't have the required arguments needed to initialize a member variable constructor until after you enter the body of your constructor.
In that case, you can create the instance dynamically.

Example:

class CharClass
{
public:
     CharClass (char* Name);
     ~CharClass();
     CharStatsClass *CharStats; //Use pointer instead of concrete type
     char* GetName();
private:
     char* PName;
};


CharClass::CharClass(char* Name)
{
    int Calculations;
    //Make code calculations here
    CharStats = new CharStatsClass(Calculations);
    PName = Name;
}

If you use this method, don't forget to delete the object in your destructor
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.