Reputation: 269
I have this annoying error in my program.
"Vehicle" is the Base class. "Bicycle" extends this class.
#ifndef BICYCLE_H
#define BICYCLE_H
#include "Vehicle.h"
#include <string>
#include <iostream>
using namespace std;
//Template class which is derived from Vehicle
template<typename T>
class Bicycle: public Vehicle
{
public:
Bicycle();
Bicycle(int, int, string, int);
~Bicycle();
//Redefined functions inherited from Vehicle
void move(int, int); // move to the requested x, y location divided by 2
void set_capacity(int); // set the capacity value; can't be larger than 2
};
Above is the Bicycle.h file (I do not have .cpp file for this class)
#ifndef VEHICLE_H
#define VEHICLE_H
#include "PassengerException.h"
#include <string>
#include <iostream>
using namespace std;
//ADD LINE HERE TO MAKE IT A TEMPLATE CLASS
template<typename T>
class Vehicle
{
public:
Vehicle(); //default contstructor
Vehicle(int, int, string, int); // set the x, y, name, and capacity
virtual ~Vehicle(); //destructor; should this be virtual or not???
//Inheritance - question #1; create these functions here and in Bicycle class
string get_name(); // get the name of the vehicle
void set_name(string); //set the name of the vehicle
void print(); // std print function (GIVEN TO YOU)
//Polymorphism - question #2
virtual void move(int, int); // move to the requested x, y location
virtual void set_capacity(int); // set the capacity value
//Operator overloading - question #3
Vehicle<T> operator+(Vehicle<T> &secondVehicle) const;
//Exceptions - question #4
T get_passenger(int) throw(PassengerException); // get the passenger at the specified index
void add_passenger(T) throw(PassengerException); // add passenger and the current passenger index
void remove_passenger() throw(PassengerException); // remove a passenger using current passenger index
protected:
int x_pos;
int y_pos;
string name;
int capacity;
T *passengers;
int current_passenger;
};
Above is the Vehicle.h file. I do not have .cpp for this either.
Also, what do the ifndef define endif mean? Do I have to use those? Are they required?
And, do their names have to be formatted like that?
Upvotes: 0
Views: 678
Reputation: 61910
class Bicycle: public Vehicle
Vehicle is a template, so you need this:
class Bicycle: public Vehicle<T>
The #ifndef and #define and #endif are called header guards and are used to prevent your header file from being included more than once, causing things (classes) to be declared more than once.
Upvotes: 2
Reputation:
You have to put #endif
at the end of your header files. These are so called define guards to prevent multiple inclusion of header files. See more at Include guard.
Upvotes: 0
Reputation: 60
The ifndef define and endif are necessary for the actual base files, i.e. the c++ files themselves. Yes they are required, if you plan to use those functions and variables accordingly. Yes, their names have to be formatted that way, that's the way directives or in some cases flags must be formatted.
Upvotes: 0