我是C ++新手。我在另一个类中继承了一个类,但是我收到一条错误消息:
error: no matching function for call to ‘Extended_queue::append(const Plane&)’
我做错了什么?我的部分代码如下:
#include<iostream>
using namespace std;
#include "runway.h"
Runway::Runway(int limit)
/*
Post: The Runway data members are initialized to record no
prior Runway use and to record the limit on queue sizes.
*/
{
queue_limit = limit;
num_land_requests = num_takeoff_requests = 0;
num_landings = num_takeoffs = 0;
num_land_refused = num_takeoff_refused = 0;
num_land_accepted = num_takeoff_accepted = 0;
land_wait = takeoff_wait = idle_time = 0;
}
Error_code Runway::can_land(const Plane ¤t)
/*
Post: If possible, the Plane current is added to the
landing Queue; otherwise, an Error_code of overflow is
returned. The Runway statistics are updated.
Uses: class Extended_queue.
*/
{
Error_code result;
if (landing.size() < queue_limit)
result = landing.append(current);
else
result = fail;
num_land_requests++;
if (result != success)
num_land_refused++;
else
num_land_accepted++;
return result;
}
错误在result = landing.append(current);
行
头部queue.h在runway.h中引用。我在下面列出了queue.h
const int maxqueue = 10; // small value for testing
struct Queue_entry {
struct Queue_entry *next;
struct Queue_entry *prev;
};
typedef struct Queue_entry *queue_t;
typedef struct Queue_entry queue_head_t;
typedef struct Queue_entry queue_chain_t;
typedef struct Queue_entry *queue_entry_t;
class Queue {
public:
Queue();
bool empty() const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item) const;
protected:
int count;
int front, rear;
Queue_entry entry[maxqueue];
};
class Extended_queue: public Queue {
public:
bool full() const;
int size() const;
void clear();
Error_code serve_and_retrieve(Queue_entry &item);
};
runway.h的代码如下所示
#include "plane.h"
#include "queue.h"
enum Runway_activity {idle, land, takeoff};
class Runway {
public:
Runway(int limit);
Error_code can_land(const Plane ¤t);
Error_code can_depart(const Plane ¤t);
Runway_activity activity(int time, Plane &moving);
void shut_down(int time) const;
private:
Extended_queue landing;
Extended_queue takeoff;
int queue_limit;
int num_land_requests; // number of planes asking to land
int num_takeoff_requests; // number of planes asking to take off
int num_landings; // number of planes that have landed
int num_takeoffs; // number of planes that have taken off
int num_land_accepted; // number of planes queued to land
int num_takeoff_accepted; // number of planes queued to take off
int num_land_refused; // number of landing planes refused
int num_takeoff_refused; // number of departing planes refused
int land_wait; // total time of planes waiting to land
int takeoff_wait; // total time of planes waiting to take off
int idle_time; // total time runway is idle
};
Runway.h引用plane.h,其中包括:
enum Plane_status {
null, arriving, departing
};
class Plane {
public:
Plane();
Plane(int flt, int time, Plane_status status);
void refuse() const;
void land(int time) const;
void fly(int time) const;
int started() const;
private:
int flt_num;
int clock_start;
Plane_status state;
};
为什么编译器无法识别Queue中Extended_queue继承的方法?
答案 0 :(得分:1)
您的错误是您正在将Plane对象传递给
Extended_queue :: append(const Queue_entry&amp; item)
其中 Extended_queue 是父类队列的子类。
无论哪种方式,您都为函数提供了错误的参数类型。这就像将 int 传递给正在调用 Plane 的函数。