我创建的程序不断出现未解决的外部符号错误,我无法弄清楚我哪里出错了。
即将出现的错误是:
错误LNK1120:1个未解析的外部
错误LNK2019:未解析的外部符号" public:__ thiscall linkedList :: ~linkinList(void)"函数" void __cdecl`动态atexit析构函数,用于' list'(void)" (?? __ Flist @@ YAXXZ)
非常感谢任何帮助。
source.cpp
#include <iostream>
#include <string>
#include "LList.h"
#include "vehicle.h"
#include "windows.h"
#include <iomanip>
using namespace std;
linkedList list;
int main()
{
char menuSelect;
do
{
menuSelect = NULL;
cout << "Please press one of the following options:";
cout << "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
cout << "\n1. List all vehicles";
cout << "\n2. Add a vehicle (Car or Van)";
cout << "\n3. Remove a vehicle";
cout << "\n4. Book a car";
cout << "\n5. Book a van";
cout << "\n6. Display a vehicle's details";
cout << "\n7. List all cars currently not rented";
cout << "\n8. List all 5-door cars";
cout << "\n9. List all Ford vans currently rented";
cout << "\n0. Quit";
cout << "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
cin >> menuSelect;
void addNode();
void deleteNode();
void getVehicles();
switch (menuSelect)
{
case '1':
getVehicles(); //displays all vehicles in the list.
break;
case '2':
addNode(); //allows the user to add another vehicle.
break;
case '3':
deleteNode(); //allows the user to delete a vehicle.
break;
case '4':
//allows the user to book a car.
break;
case '5':
//allows the user to book a van.
break;
case '6':
//allows the user to select a vehicle and show it's details.
break;
case '7':
//lists all the vehicles that are currently not rented.
break;
case '8': //lists all cars that are 5 door.
break;
case '9': //lists all of the Ford vans that are currently rented.
break;
case '0':
system("pause");
return 0;
break;
default: cout << "\n" << menuSelect << " is not a valid selection.";
cout << endl;
}
}
while (menuSelect != 0);
return 0;
}
void addNode()
{
node* newNode = new node;
cout << "Is it a car or van?: \n";
cin >> newNode->make;
cout << "\nEnter model: \n";
cin >> newNode->model;
cout << "\nEnter engine size: \n";
cin >> newNode->engine;
cout << "\nEnter registration number: \n ";
cin >> newNode->registration;
list.insertNode(newNode);
}
void deleteNode()
{
char searchData;
cout << "Enter the registration plate to be deleted: ";
cin >> searchData;
if (list.deleteNode(searchData))
cout << "\nNode deleted. \n\n\n";
cout << "\nThe registration plate is not in the list\n\n";
}
void getVehicles()
{
string searchData;
list.displayList();
}
LLIST.H
#include <string>
#include "vehicle.h"
#include "windows.h"
#include <iostream>
using namespace std;
struct node
{
int registration;
double engine;
string model;
string make;
node* nextNode;
int data;
};
class linkedList
{
private:
node* head;
public:
linkedList() { head = NULL; }
void insertNode(node*);
void searchNode(int);
bool deleteNode(int deleteVehicle);
void displayList();
~linkedList();
};
Llist.cpp
#include <iostream>
#include "windows.h"
#include <string>
#include "LList.h"
#include "vehicle.h"
#include <iomanip>
using namespace std;
vehicle.h
#pragma once
#include <string>
#include "windows.h"
//#include "LList.h"
template<class registration = int>
class Vehicle
{
public:
typedef enum { Car, Van } vehicleType;
protected:
vehicleType make;
char model;
bool rent;
double engineSize;
int registration;
public:
Vehicle(vehicleType make) : model(""), registration(reg), engineSize(engine), make(make), rented(false){}
char getMakeModel();
bool getRented;
bool setRented(bool rent);
int getEngineSize();
int getRegistration();
~Vehicle();
void listVehicle();
void removeVehicle(int deleteVehicle);
void addVehicle();
void bookVehicle();
void displayDetails(int registration);
void listNonRented();
void listFiveDoor();
void listRentedFordVan();
};
void linkedList::insertNode(node* newNode)
{
newNode->nextNode = head;
head = newNode;
}
bool linkedList::deleteNode(int deleteVehicle)
{
node* beforeNode = head;
node* thisNode = head;
bool found = false;
while ((thisNode != NULL) && !found)
{
if (thisNode->data == deleteVehicle)
found = true;
else
{
beforeNode = thisNode;
thisNode = thisNode->nextNode;
}
}
if (found)
{
if (thisNode == head)
{
node* oldHead = head;
head = head->nextNode;
delete oldHead;
}
else
{
beforeNode->nextNode = thisNode->nextNode;
delete thisNode;
}
return true;
}
return false;
}
void linkedList:: displayList()
{
node* thisNode = head;
if (head == NULL)
{
cout << "\nThe list is empty\n";
}
else
cout << "\tMake\tModel\tRegistration Number\tEngine Size\tRented?";
do
{
cout << setw(30) << left << thisNode->make;
cout << setw(25) << left << thisNode->model;
cout << setw(20) << left << thisNode->registration;
cout << setw(15) << left << thisNode->engine;
//cout << setw(10) << left << thisNode->rented;
} while (thisNode != NULL);
{
cout << "\n\n";
}
}
答案 0 :(得分:1)
问题出在这一行:
~linkedList();
在这里,您为linkedList
声明了析构函数,但没有您定义它的地方。
尝试写作
~linkedList() = default;
或者,使用较早的c++
标准,
~linkedList() {};
或者只是完全删除这一行 - 编译器将自动为您定义一个具有默认行为的析构函数。
请注意默认析构函数不会删除任何指针成员,因此,在这种情况下,您将发生内存泄漏(head
未删除和取消分配)。这可以通过多种方式修复,例如,使用std::unique_ptr<linkedList>
而不是原始指针或在析构函数体中显式写delete head
。