双星号语法&帮助调用成员函数

发布于 2024-12-12 06:00:33 字数 1636 浏览 0 评论 0原文

我正在做一个有航空课程的项目。 Airline 类包含动态 Flight 对象数组的变量。

在我的 Airlines 类中(无法编辑或更改,这是为我分配的头文件):

//Airline.h    
class Airline {
public: 
    Airline();              // default constructor
    Airline(int capacity);  // construct with capacity n
    void cancel(int flightNum);     
    void add(Flight* flight);
    Flight* find(int flightNum);
    Flight* find(string dest);

    int getSize();


private:
    Flight **array;        // dynamic array of pointers to flights
    int capacity;          // maximum number of flights
    int size;              // actual number of flights

    void resize();
};

//Flight.h
    class Flight {
public:
    Flight();
    // Default constructor

    Flight(int fnum, string destination);     
    void reserveWindow();    
    void reserveAisle();
    void reserveSeat(int row, char seat);

    void setFlightNum(int fnum);

    void setDestination(string dest);

    int getFlightNum();

    string getDest();

protected:
    int flightNumber;
    bool available[20][4];    //only four seat types per row; only 20 rows
    string destination;
 };

我正在尝试实现类中的查找方法之一。

为此,我有:

    Flight* Airline::find(int flightNum){
    bool found = false;
    for(int i = 0; i < size; i++){
        if(array[i].getflightNum() == flightNum){
            found = true;
            return array[i];
        }
    }

    if(!found)
        return 0;
}

// Return pointer to flight with given number if present, otherwise
// return 0.

但它说我在尝试调用 getFlightNum() 方法时需要一个类类型。我真的不明白这个错误。我没有正确调用该方法吗?正确的语法是什么?

I'm working a program where there's an Airline class. The Airline class contains a vairable to an array of dynamic Flight objects.

In the Airlines class I have (which can't be edited or changed, this was the header file given to me for the assignment):

//Airline.h    
class Airline {
public: 
    Airline();              // default constructor
    Airline(int capacity);  // construct with capacity n
    void cancel(int flightNum);     
    void add(Flight* flight);
    Flight* find(int flightNum);
    Flight* find(string dest);

    int getSize();


private:
    Flight **array;        // dynamic array of pointers to flights
    int capacity;          // maximum number of flights
    int size;              // actual number of flights

    void resize();
};

//Flight.h
    class Flight {
public:
    Flight();
    // Default constructor

    Flight(int fnum, string destination);     
    void reserveWindow();    
    void reserveAisle();
    void reserveSeat(int row, char seat);

    void setFlightNum(int fnum);

    void setDestination(string dest);

    int getFlightNum();

    string getDest();

protected:
    int flightNumber;
    bool available[20][4];    //only four seat types per row; only 20 rows
    string destination;
 };

I'm trying to impliment one of the find methods in the class.

for that, I have:

    Flight* Airline::find(int flightNum){
    bool found = false;
    for(int i = 0; i < size; i++){
        if(array[i].getflightNum() == flightNum){
            found = true;
            return array[i];
        }
    }

    if(!found)
        return 0;
}

// Return pointer to flight with given number if present, otherwise
// return 0.

But it's saying that I need a class type when trying to call the getFlightNum() method. I don't really understand the error. Am I not calling the method correctly? What is the correct syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-12-19 06:00:33

由于您正在处理指针而不是实际对象,请尝试以下操作:

if(array[i]->getflightNum() == flightNum){     // Notice I am using -> instead of .
    found = true;
    return array[i];
}

Since you are dealing with pointers instead of actual objects, try this:

if(array[i]->getflightNum() == flightNum){     // Notice I am using -> instead of .
    found = true;
    return array[i];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文