-1

Hello I am trying to create a program but am having some trouble I have 3 classes called Goku vegeta Beerus with Goku being the base class and vegeta and Beerus inherting from Goku I want to create an object of vegeta called rose and an object of Beerus called god inside Goku but am getting error can anyone point out how i would do this any and all help is appreciated the program is below:

class Goku
{    public:
     Goku(); 
   ˜ Goku();
   string get_Name(void); 
   int get_power(void)
 // This is what i want to do but keep getting errors
   vegeta* rose;
   Beerus* god;


};
class vegeta: public Goku
{
  public:
    vegeta(); //an intitializtion constructor
 ˜ vegeta();//destructor
   string get_Name(void); 
   int get_power(void)

};
class Beerus: public Goku
{      public:
       Beerus(); //an intitializtion constructor
      ˜ Beerus();//destructor
      string get_Name(void); 
     int get_power(void)

};

Note the error i get are: vegeta cannot name a type Beerus cannot name a type

LOLtricker
  • 13
  • 2
  • Have you tried "rubber duck debugging"? When you hit the line `GokuBlack* rose;` Do you have any idea what `GokuBlack` is? – John3136 Sep 11 '16 at 11:47
  • Oh sorry Gokublack is a mistake should be vegeta i will edit it straight away thank you for pointing it out – LOLtricker Sep 11 '16 at 11:48
  • Same question applies: when you hot `Beerus* god;`, do you have any idea what `Beerus` is? – John3136 Sep 11 '16 at 11:52

3 Answers3

1

use pre-declaring class
A class definition may reside somewhere else. But this class can be used for a pointer to a memory area.

class Beerus;         // pre-declaring class
class GokuBlack;      // pre-declaring class

class Goku
{
public:
    Goku();
    ~Goku();

    string get_Name(void);
    int get_power(void);


    GokuBlack* rose;
    Beerus* god;
};

class vegeta : public Goku
{
public:
    vegeta(); //an intitializtion constructor
    ~vegeta();
    string get_Name(void);
    int get_power(void);
};

class Beerus : public Goku
{
public:
    Beerus(); //an intitializtion constructor
    ~Beerus();

    string get_Name(void);
    int get_power(void);
};
0

You have an error in your class. Currently, when you do this:

vegeta* rose;
Beerus* god;

At that point, the compiler doesn't know what vegeta and Beerus are. To solve this, add the following 2 lines all the way at the beginning of your code:

class Beerus;    
class vegeta;

This tells the compiler, that if one of the above 2 classes is referenced, before being completely defined, as in instantiated, then the compiler can look ahead for the name in the rest of the file.


Live Example
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
0

let's not talk about containment just about forwarding declaration consider:

#include "stdafx.h"
#include <iostream>
using namespace std;


class A;
class B;

class B
{
public:
    B();
    void print() { aObj->print(); } // error
    private:
        A* aObj; // it's ok because it's declared
};

B::B()
{
    aObj = new A; // error
}

class A
{
 public:
    A(){}
    void print() { cout << "A prints" << endl; }
};

now the solution:

just move the definition of constructor B under class A and print() of B under A and everything is ok! because when it comes to initialize aObj it looks above and find a complete A this what we use when use header files containing implementations of classes so the code becomes like this:

class A;
class B;

class B
{
    public:
        B();
        void print(); // just prototype
    private:
        A* aObj;
};

class A
{
     public:
     A(){}
     void print() { cout << "A prints" << endl; }
};

B::B()
{
    aObj = new A; // correct
}

void B::print()
{
     aObj->print(); // ok
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27