简体   繁体   English

继承类的构造函数的格式

[英]Format of constructor of an inherited class

I am programming in c++ on a linux system for class. 我正在使用Linux系统上的C ++进行类编程。 I have a class Queue and a class Sim which inherits the public members of Queue. 我有一个Queue类和一个Sim类,它继承了Queue的公共成员。 Queue is compiling and testing fine. 队列正在编译和测试正常。 However when I try to code the constructor for class Sim I get an error. 但是,当我尝试为Sim类编写构造函数时,出现错误。

I have omitted irrelevant code. 我省略了无关的代码。

error: 错误:

~$ make -f p04make 〜$ make -f p04make

make: Warning: File `Sim04.cpp' has modification time 7.2 s in the future make:警告:文件“ Sim04.cpp”的修改时间为7.2 s

g++ -g -c Sim04.cpp g ++ -g -c Sim04.cpp

Sim04.cpp: In constructor âSim::Sim(int)â: Sim04.cpp:在构造函数âSim:: Sim(int)â中:

Sim04.cpp:28:64: error: no matching function for call to âQueue::Queue()â Sim04.cpp:28:64:错误:没有匹配的函数调用Queue :: Queue()

Sim04.cpp:28:64: note: candidates are: Queue04.h:27:3: note: Queue::Queue(int) Sim04.cpp:28:64:注意:候选者是:Queue04.h:27:3:注意:Queue :: Queue(int)

Queue04.h:27:3: note: candidate expects 1 argument, 0 provided Queue04.h:27:3:注意:候选人期望1个参数,提供0个

Queue04.h:19:7: note: Queue::Queue(const Queue&) Queue04.h:19:7:注意:Queue :: Queue(const Queue&)

Queue04.h:19:7: note: candidate expects 1 argument, 0 provided Queue04.h:19:7:注意:候选人期望1个参数,提供0个

make: * [Sim04.o] Error 1 make: * [Sim04.o]错误1

In Queue.h: 在Queue.h中:

class Queue {
int* Q;
int oldest;
int newest;
int size;
int count;

public:
    Queue(int sz);

In Queue.cpp: 在Queue.cpp中:

Queue::Queue(int sz = 100)
        :oldest(0), newest(-1), size(sz),count(0) 
        {Q = new int[size];}

In Sim.h: 在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();

In Sim.cpp: 在Sim.cpp中:

Sim::Sim():served(0), totalresponse(0), maxresponse(0) {}

The files are all linked into a main program file and I am compiling with a makefile. 这些文件都链接到一个主程序文件中,而我正在使用一个makefile进行编译。 I admit I do not fully understand how this constructor should be, but I modeled it off the constructors we have been using. 我承认我不完全了解该构造函数的外观,但是我根据我们一直在使用的构造函数对其进行了建模。 Am I not right in thinking that the constructor should inherit the constructor for Queue and automatically construct Sim as a Queue? 我是否认为构造函数应该继承Queue的构造函数并自动将Sim构造为Queue是不对的?

You have to call the Queue constructor from the Sim constructor with some int argument. 您必须使用一些int参数从Sim构造函数中调用Queue构造函数。 So you have to change: 因此,您必须更改:

Sim::Sim():served(0), totalresponse(0), maxresponse(0) {}

for something like: 对于类似的东西:

Sim::Sim(int sz=100):Queue(sz), served(0), totalresponse(0), maxresponse(0) {}

also you will need to modify a bit the Sim constructor declaration for receiving the arguments that Queue needs. 您还需要修改Sim构造函数声明,以接收Queue需要的参数。

Sim(int sz);

You do not have a default constructor declared for Queue. 您没有为Queue声明默认构造函数。 The constructor for Sim will automatically call the constructor for Queue during its construction. Sim的构造函数将在构造过程中自动调用Queue的构造函数。

To get this to compile you either need to implement a default constructor for Queue (one that takes no parameters) or you need to explicitly call the integer constructor for Queue (the one that takes an int) during the construction of Sim. 为了使它编译,您需要在sim的构造过程中实现Queue的默认构造函数(不带任何参数),或者需要显式调用Queue的整数构造函数(带int的整数)。

You can change in Queue.cpp 您可以在Queue.cpp中进行更改

Queue::Queue(int sz = 100) to Queue::Queue(int sz) Queue :: Queue(int sz = 100)到Queue :: Queue(int sz)

and then in Queue.h 然后在Queue.h中

class Queue {
int* Q;
int oldest;
int newest;
int size;
int count;

public:
    Queue(int sz);

to

class Queue {
int* Q;
int oldest;
int newest;
int size;
int count;

public:
    Queue(int sz = 100);

And it'll work. 而且会起作用。 You just maked mistake; 你只是犯错了; specify defauld values in headers instead of implementation. 在标头中指定默认值,而不是在实现中指定。

And one thing: it inherits whole Queue class, you can't inherit some of members. 还有一件事:它继承了整个Queue类,您不能继承某些成员。 Public means that all public fields from base class will be public in derived class too. 公共意味着基类的所有公共领域也将在派生类中也是公共的。 Private would mean that all members of base class will be private in derived, reagardless of actual scope in base class. 私有意味着基类的所有成员在派生中都是私有的,而与基类的实际范围无关。 Protected means that all public members will turn to protected members. 受保护意味着所有公共成员都将转向受保护成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM