inheritance

B

Brett Irving

I am trying to define an inherited class CheckMyArray
which inherits from InitMyArray

however it keeps coming up with

CheckMyArray.cpp: In method `CheckMyArray::CheckMyArray(int)':
CheckMyArray.cpp:6: no matching function for call to `InitMyArray::InitMyArray ()'
InitMyArray.h:15: candidates are: InitMyArray::InitMyArray(int)
InitMyArray.h:16: InitMyArray::InitMyArray(int, int)
InitMyArray.h:25: InitMyArray::InitMyArray(const InitMyArray &)
make: *** [CheckMyArray] Error 1

My code for Init and Check is

//child class of InitMyArray

#ifndef CHECKMYARRAY_H
#define CHECKMYARRAY_H

#include "InitMyArray.h"

class CheckMyArray : public InitMyArray
{
private:

int cVal;

public:

CheckMyArray(int);

void put(int, int);

};

#endif


#ifndef INITMYARRAY_H
#define INITMYARRAY_H

#include "MyArray.h"

class InitMyArray
{
private:

int arrSize;
int arrVal;

public:
/*constructors*/
InitMyArray(int);
InitMyArray(int, int);

/*Destructors*/
~InitMyArray();

/*member functions*/
int get(int);

int getSize();
};

#endif



please help, Im cant seem to figure this out.
 
J

John Harrison

Brett Irving said:
I am trying to define an inherited class CheckMyArray
which inherits from InitMyArray

however it keeps coming up with

CheckMyArray.cpp: In method `CheckMyArray::CheckMyArray(int)':
CheckMyArray.cpp:6: no matching function for call to `InitMyArray::InitMyArray ()'
InitMyArray.h:15: candidates are: InitMyArray::InitMyArray(int)
InitMyArray.h:16: InitMyArray::InitMyArray(int, int)
InitMyArray.h:25: InitMyArray::InitMyArray(const InitMyArray &)
make: *** [CheckMyArray] Error 1

My code for Init and Check is

//child class of InitMyArray

#ifndef CHECKMYARRAY_H
#define CHECKMYARRAY_H

#include "InitMyArray.h"

class CheckMyArray : public InitMyArray
{
private:

int cVal;

public:

CheckMyArray(int);

void put(int, int);

};

#endif


#ifndef INITMYARRAY_H
#define INITMYARRAY_H

#include "MyArray.h"

class InitMyArray
{
private:

int arrSize;
int arrVal;

public:
/*constructors*/
InitMyArray(int);
InitMyArray(int, int);

/*Destructors*/
~InitMyArray();

/*member functions*/
int get(int);

int getSize();
};

#endif



please help, Im cant seem to figure this out.

When you construct a CheckMyArray an InitMyArray has to be constucted first.
That is because CheckMyArray inherits from InitMyArray. To construct an
InitMyArray one of its constructors must be invoked. By default what happens
is that the default constructor gets invoked, but InitMyArray doesn't have
one, that is what the error message says.

Its hard to be certain from the code you've posted but probably you want to
do something like this

ChemMyArray::CheckMyArray(int size) : InitMyArray(size)
{
...
}

Now you are invoking the InitMyArray(int) constructor using the int
parameter for the CheckMyArray constructor.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top