Parent class is Vehicle, child class is Car. Have to use initializer list for all constructors which is proving to be a challenge for me. I keep getting the following error for what I have currently:
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") {}
^
As a novice I am mildly familiar with basic initialization lists but not when it comes to using them in child classes. In case it wasn't glaringly obvious this is for an assignment; I am just looking for help diagnosing the above error, not a full solution.
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
}
You declared and defined your constructor here
Car() : Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor") {}
therefore, the definition in the cpp file is a second definition, which is illegal
Car::Car()
{
// default constructor initialization list?
}
If you'd like to move the implementation to the cpp file, then change the header to just have the declaration
// car.h
class Car : public Vehicle
{
Car();
...
};
// car.cpp
Car::Car()
: Vehicle("NoID", -1, "NoMake", "NoModel", "NoColor")
{
// default constructor initialization list?
}