简体   繁体   English

将对象添加到链接列表C ++

[英]Adding object to a linked list c++

I am trying to add a object to my linked list, the problem is I am not sure how also the object its self is a base class with sub classes. 我试图将一个对象添加到我的链表中,问题是我不确定该对象的自身也就是带有子类的基类。 Vehicle and Car,Van respectively. 汽车和小汽车,货车。

The idea of the program is a vehicle rental system where the user can add a vehicle to the system and set certain variables about the vehicle such as: reg number engine size etc but also if the vehicle is a van or a car you the set further variables depending on the whether its a car or van. 该程序的想法是一个车辆租赁系统,用户可以在其中添加车辆,并设置有关车辆的某些变量,例如:reg号,发动机尺寸等,而且如果车辆是厢式货车还是轿车,则可以进一步设置取决于其是轿车还是面包车的变量。

For example if I want to add a Ford Van, I would enter the details such as engine size,model etc then i would specify that its a van allowing me to add further details contained within the van subclass. 例如,如果我想添加一辆福特面包车,我将输入详细信息,例如发动机尺寸,型号等,然后我将其指定为一辆面包车,从而允许我添加包含在面包车子类中的更多详细信息。

If any of you can help me with being able to add an object to a linked list such that it meets the criteria I would be very thankful. 如果有谁能帮助我将对象添加到链接列表中,使其符合标准,我将非常感激。

My code to insert a new node. 我的代码插入一个新节点。

void linkedList::InsertNode(int regNo,double engine,string model,vehicle_type make)
{
Vehicle<int> vehicle(int,double,string,vehicle_type);

//create new node
nodePtr n=new node;
//make next point to null
n->next=NULL;
n->data=vehicle(regNo,engine,model,make);

//if we have a list set current pointer
if(head!=NULL)
{
    current=head;
    //checks if end
    while(current->next!=NULL)
    {
        current=current->next;
    }
    //connect last node to new node
    current->next=n;
}
else
{
    //new node is front of list if empty
    head=n;
}
}

Linkedlist.h 链表

#pragma once
#include <string>

using namespace std;



class linkedList
{
template<class regNo>;
friend class Vehicle<int>;

typedef enum{ Car,Van} vehicle_type;

private:
typedef struct node                                                
{                                                               
  int data;               
  node* next;             
}*nodePtr;

nodePtr head;
nodePtr current;
nodePtr temp;

public:
linkedList();
void InsertNode(int regNo ,double engine,string model,vehicle_type make);
void SearchNode(int);
void PrintList();
void DeleteNode(int delData);
~linkedList();
};

vehicle.h 车辆

#pragma once

template<class regNo=int>
class Vehicle
{
public:
typedef enum{ Car,Van} vehicle_type;

protected:
vehicle_type make;
string model;
int regNo;
double engineSize;
bool rented;

public:
Vehicle(vehicle_type  make):model(""),regNo(reg),engineSize(engine),make(make),rented(false){};
char getMakeModel();
int getRegNo();
int getEngineSize();
bool getRented();
bool setRented(bool rented);
~Vehicle();

void listVehicle();
void addVehicle();
void removeVehicle(int delData);
void bookVehicle();
void displayDetails(int regNo);
void listCarsNotRented();
void listFiveDoor();
void listFordVansRented();
};

For clarification the linked listed will contain a list of many objects that holds information about a vehicle, what I need to do is add a vehicle(object) to the list. 为了清楚起见,列出的链接将包含许多包含有关车辆信息的对象的列表,我需要做的是将车辆(对象)添加到列表中。

The error occurs when trying to set the data within the node to the object I want. 尝试将节点内的数据设置为所需对象时发生错误。

First of all, I don't know how this will work: 首先,我不知道这将如何工作:

Vehicle<int> vehicle(int,double,string,vehicle_type);

I think you meant to type this: 我想您打算输入以下内容:

Vehicle<int> vehicle(regNo,engine,model,vehicle_type);

If you don't have an assignment or something for linked lists that you must implement linked lists yourself, I would recommend you to use Boost Linked lists instead of implementing your own: Link 如果您没有分配任务或必须对链表进行某些操作,则必须自己实现链表,我建议您使用Boost链表而不是自己实现:

For the actual problem itself: 对于实际问题本身:

If you want to find out what type of object it is you're adding to the linked list, simply override methods for them to support sub-classes. 如果要查找要添加到链接列表中的对象的类型,只需覆盖它们的方法以支持子类。 So instead of adding to linked list by using primitives (int, double etc.) take a Vehicle object or Van object in order to add to the list. 因此,不要使用原语(int,double等)将其添加到链表中,而是将Vehicle对象或Van对象添加到列表中。 I don't know the code but it is something like this: 我不知道代码,但它是这样的:

void linkedList::InsertNode(Van *pVan)
{
     Van newVan(*pVan); // You need a copy constructor for this
     // Set Van specific attributes like this:
     newVan->setEngineSize (123);

     //create new node
     nodePtr n = new node;
     //make next point to null
     n->next=NULL;
     n->data= newVan; // You need a copy constructor for this

     //if we have a list set current pointer
     if(head!=NULL)
     {
         current=head;
         //checks if end
         while(current->next!=NULL)
         {
             current=current->next;
         }
         //connect last node to new node
         current->next=n;
     } else
     {
         //new node is front of list if empty
         head=n;
     }
}

void linkedList::InsertNode(Car *pCar)
{
     Car newCar(*pCar); // You need a copy constructor for this
     // Set Car specific attributes like this:
     newCar->setEngineSize (123);

     //create new node
     nodePtr n = new node;
     //make next point to null
     n->next=NULL;
     n->data= newCar; // You need a copy constructor for this

     //if we have a list set current pointer
     if(head!=NULL)
     {
         current=head;
         //checks if end
         while(current->next!=NULL)
         {
             current=current->next;
         }
         //connect last node to new node
         current->next=n;
     } else
     {
         //new node is front of list if empty
         head=n;
     }
}

So override the methods to support different classes. 因此,请重写方法以支持不同的类。 When you compile the code, it will automatically call the right method for you. 编译代码时,它将自动为您调用正确的方法。 For example if you have a Van and call insertNode function that you pass a reference of your new Van object, it won't call void linkedList::InsertNode(Vehicle *pVehicle) but will call void linkedList::InsertNode(Van *pVan) 例如,如果您有Van并调用传递新Van对象的引用的insertNode函数,则它将不会调用void linkedList::InsertNode(Vehicle *pVehicle)但会调用void linkedList::InsertNode(Van *pVan)

My recommendations: - Make a "tail" pointer that points to the end of the list, so that you won't go through a while loop each time you add an object. 我的建议:-创建一个“ tail”指针,该指针指向列表的末尾,这样您每次添加对象时都不会经过while循环。 - While implementing any additional containers, use "template"s for the data types. -在实现任何其他容器时,请对数据类型使用“模板”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM