将对象实例化为类成员C ++

时间:2013-10-09 01:51:59

标签: c++ visual-c++

我有一个名为Employee的班级和名为BenefitPackage的班级。我希望BenefitPackage成为Employee的属性。该程序包含一个带有类和原型的头文件,一个包含原型定义的definitions.cpp文件和一个包含main函数的source.cpp。

我无法编译。

这是我所拥有的,提前感谢:

标题文件:

#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

class iEmployee {
public:
    virtual double calculatePay() = 0;
};
class Employee:public iEmployee {
protected:

    string firstName;
    string lastName;
    char gender;
    int dependents;
    double annualSalary;
    BenefitPackage _benefitPackage;

public:

    static int numEmployees;

    static void getEmployees() 
    {
        // gets employees up to 1000
        bool done = false;
        int empCount = 0;
        Employee employees[1000];
        do {
            Employee::numEmployees++;
            employees[empCount].setInfo();
            employees[empCount].displayAll();

        } while (done!=true && empCount < 1000);
    }

    // Below are prototypes for Employee Methods

    Employee();

    Employee(string, string, char, int, double);

    double calculatePay();

    void displayEmployee();

    void setInfo();

    string getFirstName();

    void setFirstName();

    string getLastName();

    void setLastName();

    char getGender();

    void setGender();

    int getDependents();

    void setDependents();

    void setDependents(string);

    double getAnnualSalary();

    void setAnnualSalary();

    void setAnnualSalary(string);

    void displayAll();

    static int getNumEmployees();

};

class BenefitPackage {
protected:
    string healthInsurance;
    double lifeInsurance;
    int vacation;
public:
    BenefitPackage();
    BenefitPackage(string,double,int);
    void displayBenefits();
    string getHealthInsurance();
    void setHealthInsurance(string);
    double getLifeInsurance();
    void setLifeInsurance(double);
    int getVacation();
    void setVacation(int);
};

Definitions.cpp:

#include "Header.h"
//================Below are the Definitions for the Employee Methods====================================//

    Employee::Employee():_benefitPackage(){
        firstName = "not given";
        lastName = "not given";
        gender = 'U';
        dependents = 0;
        annualSalary = 20000;       
    }
    // Construct Employee with Parameters to define attributes
    Employee::Employee(string first, string last, char gen, int dep, double salary):gender(gen),lastName(last),dependents(dep),annualSalary(salary),_benefitPackage(){};

    // Calculate Pay : Annual Salary / 52
    double Employee::calculatePay() {
        double payRate;
        payRate = annualSalary /52;
        return payRate;
    }
    // Display attributes of object
    void Employee::displayEmployee() {
        cout << "\nFirst Name: " << firstName;
        cout << "\nLast Name: " << lastName;
        cout << "\nGender: " << gender;
        cout << "\nDependents: " << dependents;
        cout << "\nSalary:\t " << setprecision(2)<<showpoint<<fixed<<annualSalary;
        cout << "\nBenefits:\n";//benefit.displayBenefits();
    }
    void Employee::setInfo() {

        setFirstName();
        setLastName();
        setGender();
        setDependents();
        setAnnualSalary();

    }
    string Employee::getFirstName() {
        return firstName;
    }
    void Employee::setFirstName() {
        cout << "\nEnter First Name: ";
        getline(cin,firstName);
    }
    string Employee::getLastName() {
        return lastName;
    }
    void Employee::setLastName() {
        cout << "\nEnter Last Name: ";
        getline(cin,lastName);
    }
    char Employee::getGender() {
        return gender;
    }
    void Employee::setGender() {
        string tempGen;
        cout << "\nEnter Gender: ";
        getline(cin, tempGen);
        gender = tempGen[0];
    }
    int Employee::getDependents() {
        return dependents;
    }
    void Employee::setDependents() {
        string tempDep;
        cout << "\nEnter Dependents: ";
        getline(cin,tempDep);
        dependents = atoi(tempDep.c_str());
    }
    void Employee::setDependents(string dep) {
        this->dependents = atoi(dep.c_str());
    }
    double Employee::getAnnualSalary() {
        return annualSalary;
    }
    void Employee::setAnnualSalary() {
        string tempSal;
        cout << "\nEnter Salary: ";
        getline(cin,tempSal);
        annualSalary = atof(tempSal.c_str());
    }
    void Employee::setAnnualSalary(string sal) {
        this->annualSalary = atof(sal.c_str());
    }
    void Employee::displayAll() {
        cout << "\n ***Employee "<<numEmployees<<" Information*** \n\n"<<"First Name: "<<firstName<<"\nLast Name: "<<lastName<<"\nGender: "<<gender<<"\nDependents: "<<dependents<<"\nSalary: "<<annualSalary<<"\n\n";
    }

    // initialize numEmployees to 0
int Employee::numEmployees = 0;

//================Below are the Definitions for the BenefitPackage Methods====================================//

// Default Constructor
BenefitPackage::BenefitPackage(){};
// Overloaded Constructor
BenefitPackage::BenefitPackage(string healthInsurance,double lifeInsurance,int vacation):healthInsurance(healthInsurance),lifeInsurance(lifeInsurance),vacation(vacation){};

void BenefitPackage::displayBenefits()
{
    cout << "\n\nHealth Insurance: "<<healthInsurance<<"\n\nLife Insurance: "<<lifeInsurance<<"\n\nVacation: "<<vacation<<"\n\n";
};
string BenefitPackage::getHealthInsurance()
{
    return healthInsurance;
};
void BenefitPackage::setHealthInsurance(string health)
{
    healthInsurance = health;
};
double BenefitPackage::getLifeInsurance()
{
    return lifeInsurance;
};
void BenefitPackage::setLifeInsurance(double life)
{
    lifeInsurance = life;
};
int BenefitPackage::getVacation()
{
    return vacation;
};
void BenefitPackage::setVacation(int vac)
{
    vacation = vac;
};

Source.cpp:

#include "Header.h"

void main() {       
    Employee::getEmployees();   
}

日志:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>Build started 10/8/2013 6:40:22 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\ConsoleApplication1.unsuccessfulbuild".
1>ClCompile:
1>  Source.cpp
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>  Definitions.cpp
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\definitions.cpp(4): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1>c:\users\me\downloads\consoleapplication1\consoleapplication1\consoleapplication1\definitions.cpp(12): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.41
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:1)

对于初学者,将您的福利包类的定义移到其他类定义之上,以便编译器知道在其他类定义中将其作为类型遇到时的好处包。

或者,将它们分成单独的头文件,因为这将使您的项目更有条理。