为链接的List c ++创建DataType

时间:2014-10-16 16:58:42

标签: c++ linked-list nodes

我正在尝试创建一个链接列表,其中节点中的数据是类型为Car的自定义数据类型,用于保存车库中车辆的到达时间和出发时间。当我运行代码时,我一直在

  

lnk2019错误:未解析的外部符号“public:__thiscall Car :: Car(void)”(?? 0Car @@ QAE @ XZ)在函数“public:__thiscall CarNode :: CarNode(void)”中引用(?? 0CarNode @@ @ QAE XZ)。

这是代码:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class Car
{
public:
    Car();

    int getArrival()
    {
        return arrival;
    };

    int getDeparture()
    {
        return departure;
    };

    int arrival = 0;
    int departure = 0;
};

class CarNode
{
public:
    Car data;
    CarNode* next;
};

class CarLinkedList
{
public:

    CarLinkedList(Car &e)  
    {
        cout << "Constructor for Car:  " << e.getArrival() << endl;
        head = new CarNode; 
        head->data = e;
        head->next = NULL;
    }

    void addFront(Car& e)       
    {
        CarNode *v = new CarNode;           
        v->data = e;                    
        v->next = head;                 
        head = v;                       
        cout << "addFront: new head is " << head->data.getArrival() << endl;
    } 
private:
    CarNode *head;              // pointer to the head of list

};

int main()
{
    Car chevy;

    CarLinkedList *s;
    s = new CarLinkedList(chevy);
    s->printSLL();

    cin.get();
}

1 个答案:

答案 0 :(得分:0)

你的代码中有这个:

class Car {
    public:
        Car();

但我没有在任何地方看到它的定义:

Car::Car() {
    ...
}