[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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.
0
List244
Asked:
List244
  • 2
3 Solutions
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now