【发布时间】:2014-05-25 10:12:24
【问题描述】:
我编写了一个简单的员工测试程序来学习如何根据用户的选择创建和初始化类对象。
我想要什么:
- 用户输入所需的员工人数
- 用户输入每个员工的名字、姓氏、出生日期、雇用日期
所以,
这是我目前所做的:
date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int = 1, int = 1, int = 1990);
~Date();
void setDate(int, int, int);
void setDay(int);
void setMonth(int);
void setYear(int);
int getDay() const;
int getMonth() const;
int getYear() const;
int checkDay(int) const;
int checkMonth(int) const;
void printDate() const;
private:
int day;
int month;
int year;
};
#endif
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
Date::Date(int d, int m, int y)
{
setDate(d, m, y);
}
void Date::setDate(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
void Date::setDay(int d)
{
day = checkDay(d);
}
void Date::setMonth(int m)
{
month = checkMonth(m);
}
void Date::setYear(int y)
{
year = y;
}
int Date::getDay() const
{
return day;
}
int Date::getMonth() const
{
return month;
}
int Date::getYear() const
{
return year;
}
int Date::checkDay(int d) const
{
const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (d > 0 && d <= daysPerMonth[getMonth()])
{
return d;
}
else if (getMonth() == 2 && d == 29 && (getYear() % 400 == 0 || (getYear() % 4 == 0 && getYear() % 100 != 0)))
{
return d;
}
else
{
cout << "Day " << d << " is invalid. Set to 1." << endl;
d = 1;
return d;
}
}
int Date::checkMonth(int m) const
{
if (m > 0 && m <= 12)
{
return m;
}
else
{
cout << "Month " << m << " is invalid. Set to 1." << endl;
m = 1;
return m;
}
}
void Date::printDate() const
{
cout << day << "-" << month << "-" << year << endl;
}
Date::~Date ()
{
cout << "Date object for date class is destroyed." << endl;
}
employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "date.h"
class Employee
{
public:
Employee(const char * = '\0', const char * = '\0', const Date & = birthDate, const Date & = hireDate);
~Employee();
void setEmployeeData(const char *, const char *, const Date &, const Date &);
void setFirstName(const char *);
void setLastName(const char *);
void setBirthDateObject(Date);
void setHireDateObject(Date);
const char *getFirstName();
const char *getLastName();
Date getBirthDateObject() const;
Date getHireDateObject() const;
void printDate() const;
private:
char first_name[25];
char last_name[25];
static Date birthDate;
static Date hireDate;
};
#endif
employee.cpp
#include <iostream>
#include "employee.h"
using namespace std;
Employee::Employee(const char *first, const char *last, const Date &dateOfBirth, const Date &dateOfHire)
{
setEmployeeData(first, last, dateOfBirth, dateOfHire);
}
void Employee::setEmployeeData(const char *first, const char *last, const Date &dateOfBirth, const Date &dateOfHire)
{
setFirstName(first);
setLastName(last);
setBirthDateObject(dateOfBirth);
setHireDateObject(dateOfHire);
}
void Employee::setFirstName(const char *first)
{
int length = strlen(first);
if (length < 25)
{
length = length;
}
else
{
length = 24;
}
strncpy(first_name, first, length);
first_name[length] = '\0';
}
void Employee::setLastName(const char *last)
{
int length = strlen(last);
if (length < 25)
{
length = length;
}
else
{
length = 24;
}
strncpy(last_name, last, length);
last_name[length] = '\0';
}
void Employee::setBirthDateObject(Date dateOfBirth)
{
birthDate = dateOfBirth;
}
void Employee::setHireDateObject(Date dateOfHire)
{
hireDate = dateOfHire;
}
const char *Employee::getFirstName()
{
return first_name;
}
const char *Employee::getLastName()
{
return last_name;
}
Date Employee::getBirthDateObject() const
{
return birthDate;
}
Date Employee::getHireDateObject() const
{
return hireDate;
}
void Employee::printDate() const
{
cout << first_name << ' ' << last_name << endl;
cout << "Hired date: "; hireDate.printDate(); cout << endl;
cout << "Birth date: "; birthDate.printDate(); cout << endl;
}
Employee::~Employee()
{
cout << "Employee object for employee class destroyed." << endl;
}
main.cpp
#include <iostream>
#include <vector>
#include "employee.h"
using namespace std;
int main()
{
const int number = 10;
char fname[25];
char lname[25];
int bd, bm, by, hd, hm, hy;
vector<Date> emp_db(number);
vector<Date> emp_hd(number);
vector<Employee> emp(number);
for (int i = 0; i < number; i++)
{
cout << "Employee " << i + 1 << endl;
cout << "Enter first name: ";
cin >> fname;
cout << "Enter last name: ";
cin >> lname;
cout << "Enter date of birth: ";
cin >> bd >> bm >> by;
cout << "Enter hire date: ";
cin >> hd >> hm >> hy;
emp_db[i].setDate(bd, bm, by);
emp_hd[i].setDate(hd, hm, hy);
emp[i].setEmployeeData(fname, lname, emp_db[i], emp_hd[i]);
}
return 0;
}
一切正常,但编译时出现链接器错误:
employee.obj:错误 LNK2001:未解析的外部符号“私有: 静态类日期员工::birthDate" (?birthDate@Employee@@0VDate@@A) main.obj:错误 LNK2001:未解决 外部符号“私人:静态类日期员工::出生日期” (?birthDate@Employee@@0VDate@@A) employee.obj:错误 LNK2001: 未解析的外部符号“私有:静态类日期 员工::hireDate" (?hireDate@Employee@@0VDate@@A) main.obj : 错误 LNK2001:未解析的外部符号“私有:静态类日期 员工::hireDate" (?hireDate@Employee@@0VDate@@A) 调试/实验室 4a.exe : 致命错误 LNK1120:2 个未解决的外部问题
我是这个行业的初学者,所以请保持冷静,告诉我我在 main() 所做的事情对吗?
更新:我怀疑main() 中的某些东西导致了它。看看我的main() 告诉我是不是这样?我的意思是,C++ 允许我在做什么吗?
【问题讨论】:
-
尽管有错误,但您有一个设计问题:在您当前的代码中,您的所有员工都有相同的年龄和招聘日期。你必须解决这个问题。
-
@FrédéricHamidi 怎么样?每次
for运行时,都会从用户那里获取出生日期和招聘日期。 -
@SilverFalcon,这些是 static 成员,它们在您的类的所有实例之间共享。在循环中分配它们只会一次又一次地覆盖它们的值,但最终所有实例在这些成员中都将具有相同的值。您需要非静态的实例成员。
-
@FrédéricHamidi 但是当我删除
static时,它会说:error C2648: 'birthDate' : use of member as default parameter requires static memberinemployee.cpp
标签: c++ visual-studio object constructor linker