【发布时间】:2014-02-19 05:46:53
【问题描述】:
我在 linux 系统上用 c++ 编程上课。我有一个类 Queue 和一个类 Sim,它继承了 Queue 的公共成员。队列正在编译和测试正常。但是,当我尝试为 Sim 类编写构造函数时,出现错误。
我省略了不相关的代码。
错误:
~$ make -f p04make
make: 警告:文件 `Sim04.cpp' 未来的修改时间为 7.2 秒
g++ -g -c Sim04.cpp
Sim04.cpp:在构造函数âSim::Sim(int)â中:
Sim04.cpp:28:64: 错误:没有匹配函数调用 âQueue::Queue()â
Sim04.cpp:28:64:注意:候选人是: Queue04.h:27:3:注意:Queue::Queue(int)
Queue04.h:27:3:注意:候选人需要 1 个参数,提供 0 个
Queue04.h:19:7: 注意:Queue::Queue(const Queue&)
Queue04.h:19:7: 注意:候选人需要 1 个参数,提供 0 个
make: * [Sim04.o] 错误 1
在 Queue.h 中:
class Queue {
int* Q;
int oldest;
int newest;
int size;
int count;
public:
Queue(int sz);
在 Queue.cpp 中:
Queue::Queue(int sz = 100)
:oldest(0), newest(-1), size(sz),count(0)
{Q = new int[size];}
在 Sim.h 中:
class Sim:public Queue{
int served;
int totalresponse;
int maxresponse;
void arrival(int time);
void departure(int time);
void Print(ostream& o, char* t, int v, char* u);
public:
Sim();
在 Sim.cpp 中:
Sim::Sim():served(0), totalresponse(0), maxresponse(0) {}
这些文件都链接到一个主程序文件中,我正在使用 makefile 进行编译。 我承认我并不完全理解这个构造函数应该是怎样的,但是我根据我们一直使用的构造函数对其进行了建模。我认为构造函数应该继承Queue的构造函数并自动将Sim构造为Queue是不是不对?
【问题讨论】:
-
你在 Sim.h 文件中
include Queue.h吗?您需要这样做,因为 Sim 类引用了 Queue 类。 -
我注意到我确实这样做了,以防以后有人需要这个答案。但是,是的,包括在内。
标签: c++ inheritance constructor