我的程序中有这个恼人的错误。
“Vehicle”是Base类。 “自行车”扩展了这一课。
#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
};
上面是Bicycle.h文件(我没有此类的.cpp文件)
#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;
};
以上是Vehicle.h文件。我也没有.cpp。
另外,ifndef定义endif是什么意思?我必须使用那些吗?他们是否需要?
而且,他们的名字必须格式化吗?
答案 0 :(得分:2)
class Bicycle: public Vehicle
Vehicle是一个模板,所以你需要这个:
class Bicycle: public Vehicle<T>
#ifndef和#define以及#endif被称为标题保护,用于防止您的头文件被多次包含,导致事物(类)被多次声明。
答案 1 :(得分:0)
ifndef define和endif对于实际的基本文件是必需的,即c ++文件本身。是的,如果您打算相应地使用这些功能和变量,则它们是必需的。是的,他们的名字必须以这种方式格式化,这是指令或在某些情况下必须格式化标志的方式。
答案 2 :(得分:0)
您必须将#endif
放在头文件的末尾。这些是所谓的定义保护,以防止多个包含头文件。请参阅Include guard。