1

I am having some issues regarding inheritance. I have a class of Person, and a class of Student:Person, Employee:Person. The errors that I am getting baffle me – I do not understand why I am getting them. I used tiny paste to paste the code as I thought it would take too much space up here. If I should post question elsewhere, let me know. Thanks.

Code Files:

Here are the errors that I am getting:

1>------ Build started: Project: PR4_Students, Configuration: Debug Win32 ------
1>Build started 2/18/2012 11:14:27 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\PR4_Students.unsuccessfulbuild".
1>ClCompile:
1>  main.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list
1>  Student.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.cpp(8): error C2084: function 'Student::Student(void)' already has a body
1>          \\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15) : see previous definition of '{ctor}'
1>  Employee.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.cpp(8): error C2084: function 'Employee::Employee(void)' already has a body
1>          \\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15) : see previous definition of '{ctor}'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:05.64
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
2
  • 1
    Welcome to SO's C++ community, Michael! In the future, earn brownie points by tagging homework-related questions with homework. Commented Feb 19, 2012 at 7:38
  • Student() : Person(); is wrong. You only need to supply the initializer-list in the definition of the constructor, not in the declaration. Try just Student();. Commented Feb 19, 2012 at 7:39

2 Answers 2

4

I didn't analyze the whole code, but you seem to be confused how to declare calls to the base class constructor;

class Student : public Person
{
...
    Student() : Person();
...
};

The call to the base class constructor should be done on the actual implementation of the constructor only. Since you already do that with

Student::Student() : Person() {

you can just change the declaration to

class Student : public Person
{
...
    Student();
...
};

and things should turn out better.

Edit: Adding the answer to a follow-up question below;

The line

Employee(string department, string jobTitle, int yearOfHire) 
  : Person(name, socialSecurityNumber, age, gender, address, phoneNumber) {

does for the same reason not really make sense. If you want to be able to construct an Employee with all those parameters, you need to instead declare the constructor as;

Employee(string department, string jobTitle, int yearOfHire, name, 
         socialSecurityNumber, age, gender, address, phoneNumber) {

and implement it as

Employee::Employee(string department, string jobTitle, int yearOfHire, name, 
         socialSecurityNumber, age, gender, address, phoneNumber) 
  : Person(name, socialSecurityNumber, age, gender, address, phoneNumber) {

thus passing on the parameters to the base class constructor.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. How would I create a Student object with the Person and Student arguments?
May totally be misunderstanding your question here, but to make it call the Person constructor with a parameter passed to the Student constructor, just change the implementation line to Student::Student(int age) : Person(age) {. Naturally, Person needs a constructor to actually call taking age as a parameter also.
Let me give you an example. Currently I think I the IDE wants this code: personVector.push_back(new Employee("Sales", "VP", 2008)); However I thought it was something like this: personVector.push_back(new Employee("Sales", "VP", 2008, "John", 123456789, 19, 'm', "Unknown", 3218424222));
@michaellindahl Added an example to the answer above.
4

At line 15 of Students.h:

Student() : Person();

That's invalid. Either you need to completely define the constructor there, or not at all.

So:

Student() : Person() { some code; };

or:

Student();

and put the actual code in your implementation file.

6 Comments

Can you have a default constructor and an overwritten constructor in the implementation file? I thought overwritten constructors needed to be in the header file.
I don't understand your question - what's an overwritten constructor? Unless you're dealing with templates, you're free to put things either in the header or in implementation files. What you can't do is a partial constructor definition like you did.
Okay. I see. I was talking about: Student(), Student(int age), and Student(string name, int age). I was told that anything other than Student() needed to be in the .h file. But according to what you are saying they actually should be in the .cpp file?
Yes. You need all the declarations (prototypes) in the header, but you can put the definitions (code) in your .cpp file. (As long as templates are not involved.)
If you want me to have this line in the .ccp then tell me what I need to have in the .h (because everything I try gives me a done of errors) Code in header: Employee(string name, int socialSecurityNumber, int age, char gender, string address, int phoneNumber, string department, string jobTitle, int yearOfHire) : Person(name, socialSecurityNumber, age, gender, address, phoneNumber) { this->department = department; this->jobTitle = jobTitle; this->yearOfHire = yearOfHire; }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.