Using other classes within classes.

I have a class Person, and a class PEmployee. I want to store an object of the class Person within the PEmployee class. Both classes are in header files, and their constructors and member functions are in separate .cpp files. I also have a main.cpp file. I've provided all the code. I am getting the error:
 
 undefined reference to 'Person::Person()'

in line 9 of PEmployee.cpp

Is this a syntax error or is my approach completely wrong? I have only just started on classes and oop, and have only really dealt with the concept of encapsulation. What is the correct way of doing this without "cheating", so to speak (using more advanced techniques)?

Person.h
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
43
  #ifndef PERSON_H
  #wdefine PERSON_H

  #include <string>

  using namespace std;

  class Person
  {
    public:
        Person();
        Person(string pname, int page);
        void get_name() const;
        void get_age() const;
    private:
        string name;
        int age;
  };

  #endif // PERSON_H

Person.cpp
[code]
  #include <iostream>
  #include "Person.h"

  using namespace std;

  Person::Person(string pname, int page)
  {
    name = pname;
    age = page;
  }

  void Person::get_name() const
  {
    cout << "Name: " << name << "\n";
  }

  void Person::get_age() const
  {
    cout << "Age: " << age << "\n";
  }


PEmployee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #ifndef PEMPLOYEE_H
  #define PEMPLOYEE_H
  #include "Person.h"

  using namespace std;

  class PEmployee
  {
    public:
        PEmployee();
        PEmployee(string employee_name, double initial_salary);
        void set_salary(double new_salary);
        double get_salary() const;
        string get_name() const;
    private:
        Person person_data;
        double salary;
  };

  #endif // PEMPLOYEE_H 


PEmployee.cpp
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
  #include "PEmployee.h"
  #include "Person.h"
  #include <iostream>
  #include <string>

  using namespace std;

  //constructor
  PEmployee::PEmployee()
  {
    person_data;
    salary = 0;
  }

  PEmployee::PEmployee(string employee_name, double initial_salary)
  {
    Person person_data(employee_name, 0);
    salary = initial_salary;
  }

  //member functions
  void PEmployee::set_salary(double new_salary)
  {
    salary = new_salary;
  }

  double PEmployee::get_salary() const
  {
    return salary;
  }

  string PEmployee::get_name() const
  {
    //return person_data.name;
    return "";
  }
undefined reference usually means you are referring to something that is undefined. In this case a default constructor Person::Person() which does not exist.

you can fix it like this so you use the constructor you did provide...
1
2
3
4
5
PEmployee::PEmployee()
    : person_data("",0)
  {
    salary = 0;
  }


or add a default constructor to Person.

Topic archived. No new replies allowed.