This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(32个答案)
4年前关闭。
我猜这个网站上的某人似乎认为这是在其他地方回答的。无论如何,我自己弄清楚了,我所要做的就是在解决方案资源管理器的“源文件”部分中包含“ EmployeeRecord.cpp”文件。现在,我遇到了另一个问题,因此我将回答这个问题,并问其他地方。
头文件:
对象文件(最重要):
主文件:
因此,基本上,我有这三个文件,在第三个文件(主文件)中使用了两个文件(对象和标头),以测试功能是否按目标文件中的注释代码所述完成了工作。我还不能发布图像,因此这是向您展示我的解决方案资源管理器的最佳尝试:
重要的是要知道我不必上交主文件,只需上交对象和标头...我对面向对象的编程非常陌生,所以我认为我的错误只是技术性错误,而不是真正的错误。代码...但也可能是我包含了析构函数的事实...
无论如何,请帮助我使用非常笨拙的措辞解决错误,因为正如我在上一段中所述,这是我第一次尝试面向对象的编程。
编辑:
你们中的许多人都要求我将其他源代码(我一直在称为目标文件)包含在“解决方案项目”中,然后给出确切的错误消息,因此它们是:
错误1错误LNK2019:无法解析的外部符号“公共:__thiscall EmployeeRecord :: EmployeeRecord(int,char *,char *,int,double)”(?? 0EmployeeRecord @@ QAE @ HPAD0HN @ Z)在函数_main C:\ Users中引用\ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj
错误2错误LNK2019:未解析的外部符号“公共:__thiscall EmployeeRecord ::〜EmployeeRecord(void)”(?? 1EmployeeRecord @@ QAE @ XZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误3错误LNK2019:未解析的外部符号“公共:int __thiscall EmployeeRecord :: getID(void)”(?getID @ EmployeeRecord @@ QAEHXZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误4错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: setID(int)”(?setID @ EmployeeRecord @@ QAEXH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误5错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: getName(char *,char *)”(?getName @ EmployeeRecord @@ QAEXPAD0 @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS中引用221 \ EmployeeRecord_main.obj
错误6错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: setName(char *,char *)”(?setName @ EmployeeRecord @@ QAEXPAD0 @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS中引用221 \ EmployeeRecord_main.obj
错误7错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: getDept(int&)”(?getDept @ EmployeeRecord @@ QAEXAAH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用.obj
错误8错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: setDept(int)”(?setDept @ EmployeeRecord @@ QAEXH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误9错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: getSalary(double *)”(?getSalary @ EmployeeRecord @@ QAEXPAN @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用.obj
错误10错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: setSalary(double)”(?setSalary @ EmployeeRecord @@ QAEXN @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误11错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: printRecord(void)”(?printRecord @ EmployeeRecord @@ QAEXXZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误12错误LNK1120:11无法解析的外部C:\ Users \ Tom \ Desktop \ CS 221 \ Debug \ CS 221.exe
和:
您的编译器可以并且将为您创建一个默认构造函数(how much does the default destructor do,请参见此处,了解它可以为您做些什么)以及一个copyconstructor和一个默认构造函数。如果您自行声明它们或将其声明为其他非默认析构函数,则不会这样做。
简而言之:
如果在.h文件中放入析构函数声明,则必须在.cpp文件中定义它!
否则,编译器将读取您的声明,并认为“哦,必须在某些cpp文件中声明它,我以后再链接它”,然后将找不到它。如果您的班级中没有任何成员属于Pointer,则不需要自定义析构函数(除非您想要任何副作用)。因此,您可以使用默认的析构函数。
(32个答案)
4年前关闭。
我猜这个网站上的某人似乎认为这是在其他地方回答的。无论如何,我自己弄清楚了,我所要做的就是在解决方案资源管理器的“源文件”部分中包含“ EmployeeRecord.cpp”文件。现在,我遇到了另一个问题,因此我将回答这个问题,并问其他地方。
头文件:
/*******************************************************************
* 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);
}
}
因此,基本上,我有这三个文件,在第三个文件(主文件)中使用了两个文件(对象和标头),以测试功能是否按目标文件中的注释代码所述完成了工作。我还不能发布图像,因此这是向您展示我的解决方案资源管理器的最佳尝试:
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:无法解析的外部符号“公共:__thiscall EmployeeRecord :: EmployeeRecord(int,char *,char *,int,double)”(?? 0EmployeeRecord @@ QAE @ HPAD0HN @ Z)在函数_main C:\ Users中引用\ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj
错误2错误LNK2019:未解析的外部符号“公共:__thiscall EmployeeRecord ::〜EmployeeRecord(void)”(?? 1EmployeeRecord @@ QAE @ XZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误3错误LNK2019:未解析的外部符号“公共:int __thiscall EmployeeRecord :: getID(void)”(?getID @ EmployeeRecord @@ QAEHXZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误4错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: setID(int)”(?setID @ EmployeeRecord @@ QAEXH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误5错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: getName(char *,char *)”(?getName @ EmployeeRecord @@ QAEXPAD0 @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS中引用221 \ EmployeeRecord_main.obj
错误6错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: setName(char *,char *)”(?setName @ EmployeeRecord @@ QAEXPAD0 @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS中引用221 \ EmployeeRecord_main.obj
错误7错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: getDept(int&)”(?getDept @ EmployeeRecord @@ QAEXAAH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用.obj
错误8错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: setDept(int)”(?setDept @ EmployeeRecord @@ QAEXH @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误9错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: getSalary(double *)”(?getSalary @ EmployeeRecord @@ QAEXPAN @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用.obj
错误10错误LNK2019:未解析的外部符号“ public:void __thiscall EmployeeRecord :: setSalary(double)”(?setSalary @ EmployeeRecord @@ QAEXN @ Z)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main中引用。对象
错误11错误LNK2019:未解析的外部符号“公共:void __thiscall EmployeeRecord :: printRecord(void)”(?printRecord @ EmployeeRecord @@ QAEXXZ)在函数_main C:\ Users \ Tom \ Desktop \ CS 221 \ EmployeeRecord_main.obj中引用
错误12错误LNK1120:11无法解析的外部C:\ Users \ Tom \ Desktop \ CS 221 \ Debug \ CS 221.exe
最佳答案
每当在.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文件中声明它,我以后再链接它”,然后将找不到它。如果您的班级中没有任何成员属于Pointer,则不需要自定义析构函数(除非您想要任何副作用)。因此,您可以使用默认的析构函数。
10-08 11:14