我想这个网站上的某个人似乎认为这个问题已在其他地方得到了解答。无论如何,我自己想通了,我所要做的就是在解决方案资源管理器的源文件部分中包含“EmployeeRecord.cpp”文件......现在我还有另一个问题,所以我会留下这个问题,并在其他地方问它。
HEADER FILE:
/*******************************************************************
* File: EmployeeRecord.h
* Name: You don't need this
* Assignment 1: Employee Record
*
* This program is entirely my own work
*******************************************************************/
// ------------------------------------------------
// EmployeeRecord.h
// Purpose: defining the EmployeeRecord class
// ------------------------------------------------
#ifndef EMPLOYEERECORD_H
#define EMPLOYEERECORD_H
#include <iostream>
#include <string>
using namespace std;
class EmployeeRecord
{
private:
// private variables
int m_iEmployeeID; // employee ID
char m_sLastName[32]; // employee last name
char m_sFirstName[32]; // employee first name
int m_iDeptID; // department ID
double m_dSalary; // employee salary
public:
// define constructors
EmployeeRecord();
EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal);
// destructor
~EmployeeRecord();
// accessor, mutator, and data manipulation funcitons
int getID();
void setID(int ID);
void getName(char *fName, char *lName);
void setName(char *fName, char *lName);
void getDept(int& d);
void setDept(int d);
void getSalary(double *sal);
void setSalary(double sal);
void printRecord();
};
#endif
对象文件(最重要的):
/*******************************************************************
* File: EmployeeRecord.cpp
* Name: You don't need this
* Assignment 1: Employee Record
*
* This program is entirely my own work
*******************************************************************/
// ------------------------------------------------
// EmployeeRecord.cpp
// Purpose: implementing the funcitons of the EmployeeRecord class
// ------------------------------------------------
#include "EmployeeRecord.h"
//constructor 1 (default)
EmployeeRecord::EmployeeRecord()
{
m_iEmployeeID = 0;
m_sLastName = "";
m_sFirstName = "";
m_iDeptID = 0;
m_dSalary = 0.0;
}
// this constructor shall set the member variables to the values passed into the function
EmployeeRecord::EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal)
{
m_iEmployeeID = ID;
m_sFirstName = *fName;
m_sLastName = *lName;
m_iDeptID = dept;
m_dSalary = sal;
}
// destructor - cleans up and deallocates any memory that pointers within this class may have referenced to
EmployeeRecord::~EmployeeRecord(){};
// function getID shall return the int value stored in the member variable m_iEmployeeID
int EmployeeRecord::getID()
{
return m_iEmployeeID;
}
// function setID will set the member variable m_iEmployeeID to the value of its' argument
void EmployeeRecord::setID(int ID)
{
m_iEmployeeID = ID;
}
// the getName() function shall copy the member variables m_sFirstName and m_sLastName into the character
// arrays pointed to by the function arguments
void EmployeeRecord::getName(char *fName, char *lName)
{
*fName = m_sFirstName;
*lName = m_sLastName;
}
// the setName() function will copy the function arguments into the member variables m_sFirstName and m_sLastName
void EmployeeRecord::setName(char *fName, char *lName)
{
m_sFirstName = *fName;
m_sLastName = *lName;
}
// the getDept() function shall be defined as a reference function. That is, a call to this function will copy the
// member variable m_iDeptID into the int variable referenced by the function argument
void EmployeeRecord::getDept(int& d)
{
d = m_iDeptID;
}
// the setDept() function will copy the function argument into the member variable m_iDeptID
void EmployeeRecord::setDept(int d)
{
m_iDeptID = d;
}
// the getSalary() function shall be defined as a pointer function. That is, a call to this function will copy the
// member variable m_dSalary into the int variable pointed to by the function argument
void EmployeeRecord::getSalary(double *sal)
{
*sal = m_dSalary;
}
// the function setSalary() shall copy the function argument into the member variable m_dSalary
void EmployeeRecord::setSalary(double sal)
{
m_dSalary = sal;
}
// this function shall print to the screen all data found in the employee's record
void EmployeeRecord::printRecord()
{
cout << "Employee ID: " << m_iEmployeeID << endl;
cout << "Last Name: " << m_sLastName << endl;
cout << "First Name: " << m_sFirstName << endl;
cout << "Department ID: " << m_iDeptID << endl;
cout << "Salary: " << m_dSalary << endl;
}
主要文件:
/*******************************************************************
* main function for project Prog1
*******************************************************************/
#include <stdio.h>
#include <time.h>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
#include "EmployeeRecord.h"
int main(void)
{
int answer, employee_id, dept_id, i;
char firstname[35], lastname[35];
double *salary;
menu:
do
{
cout << "This program is used to create and modify employee records. This program was" << endl;
cout << "created and tested by John Doe exclusively under the guidance of the" << endl;
cout << "Random School Name Here." << endl << endl;
cout << "Any attempt to copy this program and falsify its origins is punishable under" << endl;
cout << "both state and federal law." << endl << endl;
cout << "Thank you for using this program, and I hope you enjoy it." << endl << endl;
system("pause");
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
cout << " 5. Enter the Department ID into the current Employee Record" << endl << endl;
cout << " 6. Enter the Employee's Annual Salary into the current Employee Record" << endl << endl;
cout << " 7. Print the current Employee Record" << endl << endl;
cout << " 8. Quit" << endl << endl;
cin >> answer;
if (answer == 1)
{
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " Enter the Employee's ID: ";
cin >> employee_id;
cout << endl;
cout << " Enter the Employee's First Name: ";
i = 0;
cin >> firstname[i];
while (firstname[i] != '\n' && i < 35)
{
i++;
cin >> firstname[i];
}
cout << endl;
cout << " Enter the Employee's Last Name: ";
i = 0;
cin >> lastname[i];
while (lastname[i] != '\n' && i < 35)
{
i++;
cin >> lastname[i];
}
cout << endl;
cout << " Enter the Department Number: ";
cin >> dept_id;
cout << endl;
cout << " Enter the Employee's Annual Salary: $";
cin >> *salary;
//EmployeeRecord Employee1 = EmployeeRecord();
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 2)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " The current Employee ID is: " << Employee1.getID();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 3)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " Enter the Employee ID: ";
cin >> employee_id;
Employee1.setID(employee_id);
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 4)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
Employee1.getName(firstname, lastname);
cout << " Enter the Employee's First Name: ";
cin >> firstname;
cout << " and Last Name: ";
cin >> lastname;
Employee1.setName(firstname, lastname);
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 5)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
cout << " 5. Enter the Department ID into the current Employee Record" << endl << endl;
Employee1.getDept(dept_id);
cout << " Enter the Department ID: ";
cin >> dept_id;
Employee1.setDept(dept_id);
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 6)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
cout << " 5. Enter the Department ID into the current Employee Record" << endl << endl;
cout << " 6. Enter the Employee's Annual Salary into the current Employee Record" << endl << endl;
Employee1.getSalary(salary);
cout << " Enter the Employee's Annual Salary: ";
cin >> *salary;
Employee1.setSalary(*salary);
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
else if (answer == 7)
{
EmployeeRecord Employee1 = EmployeeRecord(employee_id, firstname, lastname, dept_id, *salary);
system("CLS");
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
cout << " 5. Enter the Department ID into the current Employee Record" << endl << endl;
cout << " 6. Enter the Employee's Annual Salary into the current Employee Record" << endl << endl;
cout << " 7. Print the current Employee Record" << endl << endl;
Employee1.printRecord();
system("pause");
system("CLS");
goto menu;
}
} while (answer == 1 || answer == 2 || answer == 3 || answer == 4 || answer == 5 || answer == 6 || answer == 7);
if (answer != 8)
{
system("CLS");
cout << "Invalid input! Please Choose one of the 8 options below, then press [Enter]." << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " EMPLOYEE RECORD" << endl;
cout << " Creating and Modifying an Employee Record" << endl << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << " What would you like to do?" << endl << endl;
cout << " 1. Create a new Employee Record" << endl << endl;
cout << " 2. Obtain the Employee ID of the current Employee Record" << endl << endl;
cout << " 3. Enter the Employee ID into the current Employee Record" << endl << endl;
cout << " 4. Enter the Employee's Name into the current Employee Record" << endl << endl;
cout << " 5. Enter the Department ID into the current Employee Record" << endl << endl;
cout << " 6. Enter the Employee's Annual Salary into the current Employee Record" << endl << endl;
cout << " 7. Print the current Employee Record" << endl << endl;
cout << " 8. Quit" << endl << endl;
cin >> answer;
}
else if (answer == 8)
{
system("CLS");
cout << endl << endl << endl << endl << " THANKS FOR USING THE EMPLOYEE RECORD PROGRAM" << endl << endl;
cout << "Closing program in... 5 s";
Sleep(1000);
system("CLS");
cout << endl << endl << endl << endl << " THANKS FOR USING THE EMPLOYEE RECORD PROGRAM" << endl << endl;
cout << "Closing program in... 4 s";
Sleep(1000);
system("CLS");
cout << endl << endl << endl << endl << " THANKS FOR USING THE EMPLOYEE RECORD PROGRAM" << endl << endl;
cout << "Closing program in... 3 s";
Sleep(1000);
system("CLS");
cout << endl << endl << endl << endl << " THANKS FOR USING THE EMPLOYEE RECORD PROGRAM" << endl << endl;
cout << "Closing program in... 2 s";
Sleep(1000);
system("CLS");
cout << endl << endl << endl << endl << " THANKS FOR USING THE EMPLOYEE RECORD PROGRAM" << endl << endl;
cout << "Closing program in... 1 s";
Sleep(1000);
system("CLS");
cout << endl << endl << endl << endl << " GOODBYE!" << endl << endl;
Sleep(1000);
}
}
基本上,我有这三个文件,其中两个(对象和标题)用于第三个文件(main),以测试函数是否按照目标文件中的注释代码所描述的那样完成工作。我还无法发布图片,所以这是我向您展示我的解决方案资源管理器的最佳尝试:
Solution 'CS 221' (1 project)
Solution Items
EmployeeRecord.cpp
**CS 221**
External Dependencies
Header Files
EmployeeRecord.h
Resource Files
Source Files
EmployeeRecord_main.cpp
重要的是要知道我不需要输入主文件,只需要输入对象和标题......我对面向对象编程非常新,所以我认为我的错误只是技术错误,不是真的在代码中......但也可能是因为我包含了析构函数......
无论如何,请帮助我使用非常愚蠢的修辞来解决我的错误,因为,正如我在前一段中所述,这是我第一次尝试面向对象编程。
编辑:
你们中许多人让我把我的其他源代码(我一直在调用目标文件)包含在解决方案项中,然后给你确切的错误消息,所以这里是:
错误1错误LNK2019:未解析的外部符号“public:__thiscall EmployeeRecord :: EmployeeRecord(int,char *,char *,int,double)”(?? 0EmployeeRecord @@ QAE @ HPAD0HN @ Z)在函数_main C中引用:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj
错误2错误LNK2019:函数_main C:\ Users \ Tom \ Desktop \ CS 221 \中未引用的外部符号“public:__thiscall EmployeeRecord :: ~WorkeeRecord(void)”(?? 1EmployeeRecord @@ QAE @ XZ) EmployeeRecord_main.obj
错误3错误LNK2019:未解析的外部符号“public:int __thiscall EmployeeRecord :: getID(void)”(?getID @ EmployeeRecord @@ QAEHXZ)在函数_main C中引用:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main .OBJ
错误4错误LNK2019:函数_main C:\ Users \ Tom \ Desktop \ CS 221中引用的未解析外部符号“public:void __thiscall EmployeeRecord :: setID(int)”(?setID @ EmployeeRecord @@ QAEXH @ Z) \ EmployeeRecord_main.obj
错误5错误LNK2019:未解析的外部符号“public:void __thiscall EmployeeRecord :: getName(char *,char *)”(?getName @ EmployeeRecord @@ QAEXPAD0 @ Z)在函数_main C:\ Users \ Tom \中引用Desktop \ CS 221 \ EmployeeRecord_main.obj
错误6错误LNK2019:函数_main C:\ Users \ Tom \中引用的未解析的外部符号“public:void __thiscall EmployeeRecord :: setName(char *,char *)”(?setName @ EmployeeRecord @@ QAEXPAD0 @Z) Desktop \ CS 221 \ EmployeeRecord_main.obj
错误7错误LNK2019:未解析的外部符号“public:void __thiscall EmployeeRecord :: getDept(int&amp;)”(?getDept @ EmployeeRecord @@ QAEXAAH @ Z)在函数_main C:\ Users \ Tom \ Desktop \中引用CS 221 \ EmployeeRecord_main.obj
错误8错误LNK2019:函数_main C:\ Users \ Tom \ Desktop \ CS 221中引用的未解析的外部符号“public:void __thiscall EmployeeRecord :: setDept(int)”(?setDept @ EmployeeRecord @@ QAEXH @ Z) \ EmployeeRecord_main.obj
错误9错误LNK2019:未解析的外部符号“public:void __thiscall EmployeeRecord :: getSalary(double *)”(?getSalary @ EmployeeRecord @@ QAEXPAN @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS中引用221 \ EmployeeRecord_main.obj
错误10错误LNK2019:函数_main C:\ Users \ Tom \ Desktop \ CS 221中引用了未解析的外部符号“public:void __thiscall EmployeeRecord :: setSalary(double)”(?setSalary @ EmployeeRecord @@ QAEXN @ Z) \ EmployeeRecord_main.obj
错误11错误LNK2019:未解析的外部符号“public:void __thiscall EmployeeRecord :: printRecord(void)”(?printRecord @ EmployeeRecord @@ QAEXXZ)在函数_main C中引用:C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main .OBJ
错误12错误LNK1120:11个未解析的外部C:\ Users \ Tom \ Desktop \ CS 221 \ Debug \ CS 221.exe
答案 0 :(得分:4)
每当你在.h文件中声明析构函数(或通常是任何方法)时,你必须在某处定义它。所以:
// a.h
class A {
A();
~A();
}
和
// a.cpp
A::A() {
// Do Init Stuff here.
}
A::~A() {
// Do Destructor Stuff here.
}
您的编译器可以并将为您创建一个默认构造函数(how much does the default destructor do,请参见此处了解它可以为您做什么)以及copyconstructor和默认构造函数。如果您自己声明它们或作为其他非默认析构函数,它将不会这样做。
简而言之: 如果您在.h文件中放置了析构函数声明,则必须在.cpp文件中定义它!
否则,编译器会读取你的声明并认为“哦,好吧,它必须在某个cpp文件中声明,我稍后会链接它”,然后就找不到了。如果您的班级中没有任何指针成员,则不需要自定义析构函数(除非您需要任何副作用)。在您的情况下,因此您可以使用默认析构函数 。