简体   繁体   English

类和结构,没有默认构造函数

[英]Classes & Structs, no default contructor

I'm trying to build a Linked List who's elements are of my own specified type. 我试图建立一个链表,其元素是我自己指定的类型。 Now I'm not going to lie I'm winging a lot of this having not had much experience with OOP in C++ but I'm stuck with a single error. 现在,我不会撒谎,因为我对C ++中的OOP没有太多的经验,但是我只遇到了一个错误。

My LinkedList: 我的LinkedList:

#include "Vehicle.h"
#include "string"
using namespace std;

class LinkedList
{
private:
    struct Node
    {
        Vehicle data;
        Node* next;
    };

    Node* root;
    int noofitems;

public:
    LinkedList();
    int getNoOfItems();
    Vehicle getItemByIndex(int index);
    void addItem(Vehicle itemIn);
    void deleteItem();
    void insertItem(Vehicle itemIn);
    ~LinkedList();
};

The Constructor and addItem() 构造函数和addItem()

LinkedList::LinkedList() : root(NULL), noofitems(0) {}

void LinkedList::addItem(Vehicle itemIn)
{
    Node* temp;
    temp = new Node();
    temp->data = itemIn;
    temp->next = this->root;
    this->root = temp;
}

My compiler is giving me this error: error C2512: 'LinkedList::Node' : no appropriate default constructor available . 我的编译器给我这个错误: error C2512: 'LinkedList::Node' : no appropriate default constructor available Now I've tried giving the struct a constructor like so: 现在,我尝试给struct这样的构造函数:

struct Node
{
    Vehicle data;
    Node* next;
    Node() : next(NULL) {}
};

But then I get a new error on top of the old one: IntelliSense: no default constructor exists for class "Vehicle" . 但是,在旧错误之上,我又得到了一个新错误: IntelliSense: no default constructor exists for class "Vehicle" The word constructor is starting to look wrong and I'm really frustrated. 构造函数一词开始看起来不对,我真的很沮丧。 Thanks In advance. 提前致谢。

By the way if details of the vehicle class are needed: 顺便说一句,如果需要车辆类别的详细信息:

class Vehicle
{

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

public:
    Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn);
    string getMakeModel(); // return two values concatinated
    string getRegNo();
    int getEngineSize();
    bool getRented();
    void setRented(bool rentedIn);
    ~Vehicle();
};

Vehicle::Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn) :
                make(makeIn), model(modelIn), regNo(regNoIn), engineSize(engineSizeIn),
                rented(false)
{}

string Vehicle::getMakeModel()
{
    return make + " " + model;
}

string Vehicle::getRegNo()
{
    return regNo;
}

int Vehicle::getEngineSize()
{
    return engineSize;
}

bool Vehicle::getRented()
{
    return rented;
}

void Vehicle::setRented(bool rentedIn)
{
    rented = rentedIn;
}

Vehicle::~Vehicle(){}

Node has a member of type Vehicle . Node的成员类型为Vehicle Since you cannot default construct a Vehicle the default constructor for Node is marked as deleted. 由于无法默认构造Vehicle ,因此Node的默认构造函数被标记为已删除。 You will need to provide your own default constructor that constructs the Vehicle member to some state like 您将需要提供自己的默认构造函数,以将Vehicle成员构造为某种状态,例如

struct Node
{
    Vehicle data;
    Node* next;
    Node() : data("", "", "", 0), next(nullptr) {}
};

or provide a default constructor for Vehicle like 或为Vehicle提供默认的构造函数,例如

class Vehicle
{
    //...
public:
    Vehicle() = default;
    //...
};

The error is self explanatory. 该错误不言自明。 You have not explicitly initialized vehicle in your Node class as shown: 您尚未在Node类中明确初始化vehicle ,如下所示:

struct Node
{
    Vehicle data;
    Node* next;
    Node() : next(NULL) {} // NO initialization for vehicle
};

The compiler will try and then construct a Vehicle using it's default constructor, but it finds none. 编译器将尝试使用其默认构造函数构造一个Vehicle ,但找不到任何内容。 In your vehicle class you have defined a constructor taking arguments: 在车辆类中,您定义了一个带参数的构造函数:

Vehicle(string makeIn, string modelIn, string regNoIn, int engineSizeIn);

Thus the compiler will not generate one for you. 因此,编译器不会为您生成一个。 To fix this you can either define a default constructor yourself, or you can declare one with the word default which will force the compiler too generate one: 为了解决这个问题,您可以自己定义一个默认的构造函数,也可以用default来声明一个构造函数,这将迫使编译器也生成一个:

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

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