0

プログラムにこの迷惑なエラーがあります。

「乗り物」は基本クラスです。「自転車」はこのクラスを拡張します。

#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はどういう意味ですか? それらを使用する必要がありますか?それらは必須ですか?

そして、彼らの名前はそのようにフォーマットする必要がありますか?

4

3 に答える 3

2
class Bicycle: public Vehicle

Vehicle はテンプレートなので、これが必要です。

class Bicycle: public Vehicle<T>

#ifndef と #define と #endif はヘッダー ガードと呼ばれ、ヘッダー ファイルが複数回インクルードされるのを防ぐために使用され、物 (クラス) が複数回宣言される原因となります。

于 2012-04-21T22:05:23.027 に答える
0

ifndef define と endif は、実際のベース ファイル、つまり c++ ファイル自体に必要です。はい、必要に応じてこれらの関数と変数を使用する場合は必須です。はい、それらの名前はそのようにフォーマットする必要があります。それは、ディレクティブまたは場合によってはフラグをフォーマットする必要がある方法です。

于 2012-04-21T22:04:05.733 に答える
0

#endifヘッダーファイルの最後に配置する必要があります。これらは、ヘッダー ファイルの複数のインクルードを防ぐためのガードの定義と呼ばれます。インクルードガードで詳細を参照してください。

于 2012-04-21T22:04:22.987 に答える