可视实例化对象作为类成员C++
visual Instantiating Object as Class Member C++
我有一个名为Employee
的类和一个名为BenefitPackage
的类。我希望BenefitPackage
成为Employee
的属性。 该程序由一个包含类和原型的头文件、一个包含原型定义的定义.cpp文件和一个包含 main 函数的源.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);
};
定义.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*** nn"<<"First Name: "<<firstName<<"nLast Name: "<<lastName<<"nGender: "<<gender<<"nDependents: "<<dependents<<"nSalary: "<<annualSalary<<"nn";
}
// 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 << "nnHealth Insurance: "<<healthInsurance<<"nnLife Insurance: "<<lifeInsurance<<"nnVacation: "<<vacation<<"nn";
};
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;
};
资料来源.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 "DebugConsoleApplication1.unsuccessfulbuild".
1>ClCompile:
1> Source.cpp
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Definitions.cpp
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C2146: syntax error : missing ';' before identifier '_benefitPackage'
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1header.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1definitions.cpp(4): error C2614: 'Employee' : illegal member initialization: '_benefitPackage' is not a base or member
1>c:usersmedownloadsconsoleapplication1consoleapplication1consoleapplication1definitions.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 ==========
对于初学者,请将福利包类的定义移到其他类定义之上,以便编译器在遇到福利包作为其他类定义中的类型时知道它是什么。
或者,将它们分成单独的头文件,因为这会使您的项目更有条理。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 我们可以访问一个不存在的联盟的成员吗