Question: I'm currently learning about inheritance, and I'm using SublimeText3 as an editor. I've created a simple inheritance. Vehicle as a superclass, and Car as a
I'm currently learning about inheritance, and I'm using SublimeText3 as an editor. I've created a simple inheritance. Vehicle as a superclass, and Car as a subclass. I only have one method, which is located in my Vehicle superclass that prints out how many wheels the car has. The terminal keeps giving me this error provided below. Also, below the error, I provide my main.cpp, Vehicle.cpp, Vehicle.h, Car.cpp, Car.h, and Makefile. Can anyone fix this? I'm getting really frustrated, I've been trying to make this work for a whole day, trying out a lot of experiments. I do have a hunch that I'm not including the right header files.
ERROR:
g++ -c -Werror main.cpp
g++ -c -Werror Car.cpp
Car.cpp:5:6: error: redefinition of 'Car'
Car::Car() : Vehicle(4) {
^
./Car.h:9:5: note: previous definition is here
Car() : Vehicle(4) {};
^
1 error generated.
Here are my codes
main.cpp
#include
int main(int argc, char** argv){ Car c1; Vehicle& v1 = c1; Vehicle* v2 = new Car(); // print how many wheels the car has v1.speak(); return 0; } // end of main
Vehicle.cpp
#include
Vehicle::Vehicle(int numwheels) { nwheels = numwheels; } // end of Vehicle contructor
// prints out how many wheels the car has void Vehicle::speak() const throw() { printf("The car has %d wheels ", nwheels); } // end of constructor
Vehicle.h
#ifndef VEHICLE_H #define VEHICLE_H #include
class Vehicle { int nwheels; public: Vehicle(int numwheels); void speak() const throw(); }; // end of Vehicle class
#endif
Car.cpp
#include
// constructor Car::Car() : Vehicle(4) { } // end of constructor
Car.h
#ifndef CAR_H #define CAR_H #include
class Car : public Vehicle {
public: Car() : Vehicle(4) {}; }; // end of Car class
#endif
Makefile
all:main
main.o: main.cpp Vehicle.h Car.h g++ -c -Werror main.cpp
Vehicle.o: Vehicle.cpp Vehicle.h g++ -c -Werror Vehicle.cpp
Car.o: Car.cpp Car.h g++ -c -Werror Car.cpp
main: main.o Vehicle.o Car.o g++ -o main main.o Vehicle.o Car.o
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
