如何修复此子 class' 初始化列表错误?

How do I fix this child class' initializer list error?

Parent class 是 Vehicle,子 class 是 Car。必须为所有构造函数使用初始化列表,这对我来说是一个挑战。我不断收到以下关于我目前拥有的错误:

car.cpp:7:1: error: redefinition of 'Car::Car()'
Car::Car()
^
In file included from car.cpp:3:0:
car.h:12:5: note: 'Car::Car()' previously defined here
     Car() : Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor") {}
     ^

作为新手,我对基本的初始化列表略微熟悉,但在子 classes 中使用它们时却不是。如果不是很明显,这是为了一项任务;我只是在寻求诊断上述错误的帮助,而不是完整的解决方案。

vehicle.h

#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>
#include <string>
using namespace std;

class Vehicle
{
protected:
    /* Each of these data item represent something any type of vehicle
    will have, so it makes sense these would be in the base class. */

    // added a parent constructor so child classes can use
    // these variables in initializer list
    Vehicle(string id, int year, string make, string model,
            string color) : id(id), year(year), make(make),
                            model(model), color(color) {}
    string id;
    int year;
    string make;
    string model;
    string color;

public:
    Vehicle();
    // Vehicle(string id, int year, string make, string model, string color);
    Vehicle(ifstream &infile);
    // virtual ~Vehicle();
    string getID();
    void setID(string ID);
    virtual void printInfo(ofstream &out);
};

#endif

car.h

#ifndef CAR_H
#define CAR_H

#include <iostream>
#include <string>
#include "vehicle.h"
using namespace std;

class Car : public Vehicle
{
private:
    Car() : Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor") {}
    int doors;
    string paymentType;

public:
    // Car();
    Car(string id, int year, string make, string model, string color, int doors, 
string paymentType);
    Car(ifstream &infile);

    int getDoors();
    void setDoors(int numdoors);

    string getPaymentType();
    void setPaymentType(string pt);

    void printInfo(ofstream &out);
};

#endif

car.cpp

#include <iostream>
#include <string>
#include "car.h"
#include "vehicle.h"
using namespace std;

Car::Car()
{
    // default constructor initialization list?
}

Car::Car(string id, int year, string make, string model,
         string color, int doors, string paymentType)
{
    // parameterized constructor
}

Car::Car(ifstream &infile)
{
    // regular constructor i think
}

int Car::getDoors()
{
    return doors;
}

void Car::setDoors(int numdoors)
{
    doors = numdoors;
}

string Car::getPaymentType()
{
    return paymentType;
}

void Car::setPaymentType(string pt)
{
    paymentType = pt;
}

void Car::printInfo(ofstream &out)
{
// unfinished
} 

您在此处声明了并定义了您的构造函数

Car() : Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor") {}

因此cpp文件中的定义是二次定义,是非法的

Car::Car()
{
    // default constructor initialization list?
}

如果您想将实现移至 cpp 文件,请将 header 更改为只包含声明

// car.h
class Car : public Vehicle
{
    Car();
    ...
};

// car.cpp
Car::Car()
: Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor")
{
    // default constructor initialization list?
}