c++ - 如何修复此子类的初始化程序列表错误?

标签 c++ inheritance constructor initializer-list

parent 类是车辆, child 类是汽车。必须对所有构造函数使用初始化列表,这对我来说是一个挑战。我一直收到以下错误消息:

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") {}
     ^

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

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?
}

关于c++ - 如何修复此子类的初始化程序列表错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61257785/

相关文章:

C++11:编译器何时将 {} 视为 std::initializer_list,何时不视为?

c++ - 将模板类作为模板的模板类将无法编译

C++11 - 构造函数继承

java - Java 中的继承 - "Cannot find symbol constructor"

c++ - 在派生类的构造函数中初始化父类(super class)

c++ - VS2008 内部编译错误

C++:实现智能指针

c++ - 从基于 QObject 的类继承构造函数

c++ - Eclipse CDT 中的格式(换行)构造函数初始化程序列表

oop - 确定构造函数、初始化和重置方法的任务的最佳实践是什么