2
votes

I am programming in c++ on a linux system for class. I have a class Queue and a class Sim which inherits the public members of Queue. Queue is compiling and testing fine. However when I try to code the constructor for class Sim I get an error.

I have omitted irrelevant code.

error:

~$ make -f p04make

make: Warning: File `Sim04.cpp' has modification time 7.2 s in the future

g++ -g -c Sim04.cpp

Sim04.cpp: In constructor âSim::Sim(int)â:

Sim04.cpp:28:64: error: no matching function for call to âQueue::Queue()â

Sim04.cpp:28:64: note: candidates are: Queue04.h:27:3: note: Queue::Queue(int)

Queue04.h:27:3: note: candidate expects 1 argument, 0 provided

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

Queue04.h:19:7: note: candidate expects 1 argument, 0 provided

make: * [Sim04.o] Error 1

In Queue.h:

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

public:
    Queue(int sz);

In Queue.cpp:

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

In 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::Sim():served(0), totalresponse(0), maxresponse(0) {}

The files are all linked into a main program file and I am compiling with a 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?

3
do you include Queue.h inside the Sim.h file? You need to because the Sim class references the Queue class.Damien Black
I am noting that I did do that in case anyone needs this answer later. But yes, included.Victoria Potvin

3 Answers

1
votes

You have to call the Queue constructor from the Sim constructor with some int argument. 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(int sz);
0
votes

You do not have a default constructor declared for Queue. The constructor for Sim will automatically call the constructor for Queue during its construction.

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.

0
votes

You can change in Queue.cpp

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

and then in 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. 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.