C ++类(组合和继承 - 头文件,类数组)

时间:2012-12-25 21:46:41

标签: c++ class inheritance header-files composition

我编写了4个头文件(类),其中3个类已成功编译(Customer,GoldCustomer和PlatinumCustomer)。 GoldCustomer和PlatinumCustomer是Customer的派生(继承),而有问题的MovieTicket类是所有其他3个类的组合。这是遵循强加的要求:显示继承和组成。

编辑MovieTicket会给我这些错误。任何人都可以帮助检查和解释?似乎重复了类的声明(但是从我在Google中读到的内容,只要你把类包含在头文件中并放入标题保护,那个错误就不会出来)。另一件事是,我的类数组给出了错误......我不知道为什么

In file included from class_GoldCustomer.hpp:3:0, from class_MovieTicket.hpp:5:
class_Customer.hpp:11:7: error: redefinition of ‘class Customer’

In file included from class_MovieTicket.hpp:4:0: 
class_Customer.hpp:11:7: error: previous definition of ‘class Customer’

In file included from class_PlatinumCustomer.hpp:3:0, 
from class_MovieTicket.hpp:6:  class_Customer.hpp:11:7: 
error: redefinition of ‘class Customer’

In file included from class_MovieTicket.hpp:4:0: class_Customer.hpp:11:7: 
error: previous definition of ‘class Customer’

class_MovieTicket.hpp:29:22: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:29:25: error: expected unqualified-id before ‘]’ token
class_MovieTicket.hpp:32:26: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:32:29: error: expected unqualified-id before ‘]’ token
class_MovieTicket.hpp: In member function 
‘void MovieTicket::newMovie(int, std::string,     std::string, int, int, float)’:
class_MovieTicket.hpp:53:3: error: ‘ListGold’ was not declared in this scope
class_MovieTicket.hpp:53:12: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:53:15: error: expected primary-expression before ‘]’ token
class_MovieTicket.hpp:53:15: error: expected ‘;’ before ‘]’ token
class_MovieTicket.hpp:54:3: error: ‘ListPlat’ was not declared in this scope
class_MovieTicket.hpp:54:12: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:54:15: error: expected primary-expression before ‘]’ token
class_MovieTicket.hpp:54:15: error: expected ‘;’ before ‘]’ token
class_MovieTicket.hpp: In member function ‘void MovieTicket::Purchase()’:
class_MovieTicket.hpp:103:4: error: ‘ListGold’ was not declared in this scope
class_MovieTicket.hpp:108:4: error: ‘ListPlat’ was not declared in this scope
Compilation failed.

以下是代码:

class_Customer.hpp

#include <iostream>
#include <string>

//header guard
#ifndef CUSTOMER_H
#define CUSTOMER_H

using namespace std; 

class Customer {

protected:
//data members
int id;
string type;
int sold;

//public:
//constructor and destructor
Customer(){
    type = "non member"; //default this customer class as 'non member'
    sold = 0;
}
~Customer(){}

//functions member
void setID(int inputID){ //set ID of the customer
    id = inputID;
}

int getSold(){ //get no. of sold
    return sold;
}

};
#endif[/CODE]

class_GoldCustomer.hpp

#include <iostream>
#include <string>
#include "class_Customer.hpp"

using namespace std; 

//header guard
#ifndef GOLDCUSTOMER_H
#define GOLDCUSTOMER_H


class GoldMember : protected Customer{

private:
//data members
int gold_id;
int annual_fee;
float discount;

public:
//constructor and destructor
GoldMember(){
    type = "gold"; 
    sold = 0;
    annual_fee = 10;
    discount = 0.1;
}
~GoldMember(){}

//functions member
int getAnnualFee(){ //get annual fee
    return annual_fee;
}

float getDiscount(){ //get discount
    return discount;
}

};
#endif[/CODE]

class_PlatinumCustomer.hpp

#include <iostream>
#include <string>
#include "class_Customer.hpp"

#define POINT 5; //how many ticket purchased needed to eligible for one free ticket

using namespace std; 

//header guard
#ifndef PLATINUMCUSTOMER_H
#define PLATINUMCUSTOMER_H

class PlatinumMember : protected Customer{

private:
//data members
int plat_id;
int annual_fee;
float discount;
int redeem_point = 0; //count the tickets purchased as 'points'

public:
//constructor and destructor
PlatinumMember(){
    type = "gold"; 
    sold = 0;
    annual_fee = 50;
    discount = 0.2;
    redeem_point = 0;
}
~PlatinumMember(){}

//functions member
int getAnnualFee(){ //get annual fee
    return annual_fee;
}

float getDiscount(){ //get discount
    return discount;
}

int getFRedeemPoint(){ //get redeem point
    return redeem_point;
}

int getFreeTicket(){ //get no. of free tickets based on redeem point
    int result;
    result = redeem_point/POINT;
    return result;
}

};
#endif[/CODE]

最后,编译失败:

class_MovieTicket.hpp

#include <iostream>
#include <string>

#include "class_Customer.hpp"
#include "class_GoldCustomer.hpp"
#include "class_PlatinumCustomer.hpp"

//header guard
#ifndef MOVIETICKET_H
#define MOVIETICKET_H


using namespace std; 

#define MAX 20; //no of tickets per movie hall available

class MovieTicket {

private:
int movie_id;
string movie_title;
string date;
int hall_no;
int seat_no;
float ori_price;

int NonMemberCount;

GoldMember ListGold[MAX];
int GoldMemberCount;

PlatinumMember ListPlat[MAX];
int PlatMemberCount;

static int AvailTicket;
static int SoldTicket;

public:
//constructor and destructor
MovieTicket(){};
~MovieTicket(){};

//functions member
void newMovie(int inputMovieID, string inputTitle, 
 string inputDate, int inputHall,    int inputSeat, float inputPrice){

    movie_id = inputMovieID;
    movie_title = inputTitle;
    date = inputDate;
    hall_no = inputHall;
    seat_no = inputSeat;
    ori_price = inputPrice;

    ListGold[MAX] = {0};
    ListPlat[MAX] = {0};

    AvailTicket = MAX;
    SoldTicket = 0;
}

int getAvailTicket(){
    return AvailTicket;
}

int getSoldTicket(){
    return SoldTicket;
}

void Purchase(){
    int no_of_ticket;
    int membership;
    int Inputgold_id;
    int Inputplat_id;

    cout <<"Enter Movie ID?" << endl;
    cin >> movie_id;
    cout << endl << "How many ticket?" << endl;
    cin >> no_of_ticket;
    cout << endl << "Enter membership? 1. Non 2. Gold 3.Plat" << endl;
    cin >> membership;

    this->SoldTicket += no_of_ticket;
    this->AvailTicket = MAX - this->SoldTicket;


    if (membership == 2){
        cout << "Enter your Gold Membership ID?" << endl;
        cin >> Inputgold_id;
        GoldMember Inputgold_id; 
                     //create a Gold customer class with 'ID' as the name
    } 
    else if (membership == 3){
        cout << "Enter your Platinum Membership ID?" << endl;
        cin >> Inputplat_id;
        PlatinumMember Inputplat_id; 
                    //create a Plat customer class with 'ID' as the name
    }

    switch (membership){
        case 1:
        NonMemberCount++;
        break;

        case 2:
        GoldMemberCount++;
        ListGold[GoldMemberCount] = Inputgold_id;
        break;

        case 3:
        PlatMemberCount++;
        ListPlat[PlatMemberCount] = Inputplat_id;
        break;
    }
}
};
#endif[/CODE]

p / s:很抱歉这篇长篇文章。

2 个答案:

答案 0 :(得分:1)

您的编译命令行是什么?你是否将头文件提供给编译器?你应该有一个虚拟的main.cpp包含所有东西,只编译main.cpp

答案 1 :(得分:0)

你当然不应该有分号:#define MAX 20;

这导致PlatinumMember ListPlat[MAX];扩展到PlatinumMember ListPlat[20;];,这就是编译器呻吟的原因。

还有其他几个地方有类似的#define

我建议使用'const int MAX = 20; (preferrably max = 20`,因为大写表示宏。

快速浏览一下,其他错误只是上述错误的结果 - 编译器在遇到奇怪的东西时会有点丢失,只是在没有声明变量的情况下继续,然后变量就会丢失。

编辑:顺便说一句,在您的“platinumMember.hpp”中,type = "gold";不应该是type = "platinum";吗?

(Darn,太慢了!)