0

I recently started learning object oriented programming..for the following code, I am getting the linker error !!

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class person{
    public:
        string name;
        person();
        person(string n){
            name = n ;
        }

        void setName(string k){
            name = k;
        }
        string getName(){
            return name;
        }
};
class student : public  person {
    public:
        string major;
        void setMajor(string m){
            major = m;
        }
        string getMajor(){
            return major;
        }
};
class faculty : public person{
    public:
        string department;
        faculty(string dept){
            department = dept;
        }
        void setDepartment(string depart){
            department = depart;
        }
        string getDepartment(){
            return department;
        }
};

int main() {

    student s;

    s.setName("james");
    s.setMajor("computer science");

    string p = s.getName();
    string p2 = s.getMajor();

    cout << "student name and mjor is :" << p << p2 << endl;
    faculty f("nanotech");
    f.setName("chris");
    f.setDepartment("electrical");
    string f1 = f.getName();
    string f2 = f.getDepartment();
    cout << "facult name and department :" << f1 << f2 << endl;

    return 0;
}

The following error i am getting , when i try to compile the code.

/tmp/ccYHu2de.o: In function `faculty::faculty(std::string)':
person.cpp:(.text._ZN7facultyC2ESs[_ZN7facultyC5ESs]+0x19): undefined reference to `person::person()'
/tmp/ccYHu2de.o: In function `student::student()':
person.cpp:(.text._ZN7studentC2Ev[_ZN7studentC5Ev]+0x15): undefined reference to `person::person()'
collect2: error: ld returned 1 exit status
xsami
  • 1,312
  • 16
  • 31
robert williams
  • 747
  • 2
  • 11
  • 18

2 Answers2

0

In your class person you have a constructor that is declared but is not defined. The compiler let you compile the class but then the linker looks for an implementation (which is called forward declaration):

class person{
   public:
      string name;
      person();        //constructor only declared
      person(string n){
          name = n ;
     }
     .....    
 };

The problem is that you inherit from person in your student and faculty, you use the default constructor but you never implement it. Try to add an implementation and the linker error will disappear:

class person{
       public:
          string name;
          person(){}        //constructor implemented
          person(string n){
              name = n ;
         }
         .....    
     };
codingadventures
  • 2,924
  • 2
  • 19
  • 36
0

The Problem is that you need to implement the constructor of the person class, Try this code: http://ideone.com/NfXP7R

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class person{
    public:
        string name;
        person() {

        }
        person(string n){
            name = n ;
        }

        void setName(string k){
            name = k;
        }
        string getName(){
            return name;
        }
};
class student : public  person {
    public:
        string major;
        void setMajor(string m){
            major = m;
        }
        string getMajor(){
            return major;
        }
};
class faculty : public person{
    public:
        string department;
        faculty(string dept){
            department = dept;
        }
        void setDepartment(string depart){
            department = depart;
        }
        string getDepartment(){
            return department;
        }
};

int main() {

    student s;

    s.setName("james");
    s.setMajor("computer science");

    string p = s.getName();
    string p2 = s.getMajor();

    cout << "student name and mjor is :" << p << p2 << endl;
    faculty f("nanotech");
    f.setName("chris");
    f.setDepartment("electrical");
    string f1 = f.getName();
    string f2 = f.getDepartment();
    cout << "facult name and department :" << f1 << f2 << endl;

    return 0;
}
xsami
  • 1,312
  • 16
  • 31