Base Class Undefined Problem

Hello guys i am trying to fix my code for past 5 hours and cannot find a solution so if any experts can help me out please.

ok so my 1 parent class(Employee) has 1 child class(Accountant).
I have object of the parent class in my other class(Controller).

The above works perfectly but once i add object of child(Accountant) in parent(Employee)class so i can access Accountant from my (Controller) class it gives me base class undefined problem.

Accountant.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef ACCOUNTANT_H
#define ACCOUNTANT_H

#include "Employee.h"
#include <string>
using namespace std;

class Accountant: public Employee
{

public:
	void someStuff();

private:
	string stuff;
};
#endif 


Accountant.cpp
1
2
3
4
5
6
#include "Accountant.h"

//Declaration code 
void Accountant::someStuff()
{}


Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include "Accountant.h"

#include <string>
using namespace std;

class Employee
{
public: 
	void setName(string);
	string getName();
	Accountant a;

private:
	string name;
};

#endif 


Employee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Employee.h"

void Employee::setName(string empName)
{
	name = empName;
}

string Employee::getName()
{
	return name;
}



controller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "Employee.h"
#include <string>

class Controller
{
public:
        Controller();
	Employee obj;

private:


};

#endif 


Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Controller.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

Controller::Controller()
{
          obj.a.someStuff() // this is what i want to do
}


I know after doing some research this is bad practice to include child object in parent class. But I want to access the child class from my controller somehow. Thanks for reading :)
Last edited on
Not only is it bad practice, it's not really possible. when you say you want to access the child class from your base class, what exactly do you mean? There's probably a way to accomplish what you want, but this is not it.
closed account (SECMoG1T)
The problem is that you can't include an object of a derived class in its parent . Reason being

1. During compilation first thing that the classes will
Be first compiled , there compiler checks to see that all names defined or declared in them are in the
scope of The classes {/*see name lookup*/} if not the then it issues an error that the
object included is not yet Defined or declared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
     class one
      {
         public:
                  one (string s1, string s2, int mrks) : marks (mrks)
                    { 
                         names.setnames (s1, s2); 
                    }
                  void print ()
                    {
                           cout <<"my names "<<names.getfirst ()<<" "<<names.getsecond ()
                                    <<" my marks "<<marks;
                    }
         private:
                 two names; 
                  int marks; 
      }

      class two
        {
           public:
                  two (string s1, s2): firstname (s1), secondname (s2){}
                  string getfirst() const {return firstname; }
                  string getsecond() const { return secondname;}
                  void setnames (string s1, string s2)
                        {
                           firstname=s1;
                           secondname=s2;
                         }
           private:
                  string firstname; 
                  string secondname;
         }

         int main ()
         {
             one student1 (string ("John"), string ("Davies"), 78);
             
             student1.print (); /// this will not work lest you provide the pprototype of
                                            /// the class two before the definition of class one.
         }
        
     


2. It s obvious that a parent class have to be defined before it child classes
the during compilation the compiler runs through the step I described above
checking that all objects declared in it are in scope or that they are defined and in scope

In our case here the compiler comes accross an object Accountant a; in the parent class
Employee. Well it tries to find the definition or declaration of such a class and doesn't find it
and if it finds it it will check on it defination and find that an object of the Accountant class
will require complete defination of the parent class Employee....
so this will ne considered an error because you have included an incomplete object in the Employee
class {Accountant a; is incomplete coz it requires a complete defination of Employee class which
is still not defined; this checking will be redudant}

note you can't include an object of a child class in the parent class as it will be an incomplete object.

3. Solution
1
2
3
so i can access Accountant from my (Controller) class it gives me base class undefined     
               problem. Accountant.
     

This could be easily done if you could make you controller class a friend of Accountant. then you can have direct access to its members That way.
Last edited on
Thanks for the help guys

I solved it by declaring object of Accountant class in Controller class so i can do this now

(in Controller class)
Account a
and now i have access to the accountant class and its parent class.

Controller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "Employee.h"
#include <string>

class Controller
{
public:
        Controller();
	Accountant obj[5];

private:


};

#endif  


Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Controller.h"
#include "Accountant.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

Controller::Controller()
{
          obj[0].someStuff(); // this fuction in accountant class
          obj[1].setName();  // this fucttion in employee class
}
{}



Now i just have to declare object as private
Topic archived. No new replies allowed.