Archived

This topic is now archived and is closed to further replies.

Templated Inheritance

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a trick to inheritance with templates? I get error C2955: ''ctBag'' : use of class template requires template argument list Here''s the code. ctBag.h, parent
  
template <class theClass>
class ctBag
{
public:
	ctBag();
	ctBag(unsigned int iLength);
	ctBag(unsigned int iLength, theClass fillValue);
	ctBag(unsigned int iLength, theClass* fillValue);
	~ctBag();
	unsigned int getLength();
protected:
	theClass** data;
	unsigned int length;
};

#include "ctBag.h"

//*********************Constructors***************************


template <class theClass>
ctBag<theClass>::ctBag(unsigned int iLength, theClass* fillValue)
: length(iLength), data(new theClass*[iLength])
{
    for (unsigned int i = 0; i < iLength; i++)
    {
        data[i] = new theClass(fillValue);
    }
}

template <class theClass>
ctBag<theClass>::ctBag(unsigned int iLength, theClass fillValue)
: length(iLength), data(new theClass*[iLength])
{
    for (unsigned int i = 0; i < iLength; i++)
    {
        data[i] = new theClass(&fillValue);
    }
}

template <class theClass>
ctBag <theClass>::ctBag() : length(10)
{
	data = new theClass*[length]();
	for (unsigned int i = 0; i < length; i++)
		data[i] = NULL;
}

template <class theClass>
ctBag <theClass>::ctBag(unsigned int iLength) : length (iLength)
{
	data = new theClass*[iLength]();
	for (unsigned int i = 0; i < length; i++)
		data[i] = NULL;
}

//*******************Destructor*********************************


template <class theClass>
ctBag <theClass>::~ctBag()
{
	for (unsigned int i = 0; i < length; i++)
	{
		if (data[i] != NULL) delete data[i];
	}
	delete[] data;
}

template <class theClass>
unsigned int ctBag <theClass>::getLength(){return length;}

  
here''s ctVector.h, the child
  
#ifndef _CTVECTOR_
#define _CTVECTOR_
//#include <stdlib.h>
#include <memory.h>
#include "ctBag.h"




template <class theClass> 
class ctVector:public ctBag
{
public:
	ctVector();
	ctVector(unsigned int iLength);
	ctVector(unsigned int iLength, theClass fillValue);
	ctVector(unsigned int iLength, theClass* fillValue);

	~ctVector();

	theClass* operator[](unsigned int index);
	theClass& operator= (theClass info);
	theClass& operator= (theClass* info);

	void put(unsigned int index, theClass* info);
	void put(unsigned int index, theClass info);
	theClass* get(unsigned int index);
	void remove(unsigned int index);

	unsigned int getLength();
protected:
	//theClass** data;

	//unsigned int length;

};

//*********************Constructors***************************


template <class theClass>
ctVector <theClass>::ctVector() : length(10)
{
	data = new theClass*[length]();
	for (unsigned int i = 0; i < length; i++)
		data[i] = NULL;
}

template <class theClass>
ctVector<theClass>::ctVector(unsigned int iLength, theClass* fillValue)
: length(iLength), data(new theClass*[iLength])
{
    for (unsigned int i = 0; i < iLength; i++)
    {
        data[i] = new theClass(fillValue);
    }
}

template <class theClass>
ctVector<theClass>::ctVector(unsigned int iLength, theClass fillValue)
: length(iLength), data(new theClass*[iLength])
{
    for (unsigned int i = 0; i < iLength; i++)
    {
        data[i] = new theClass(&fillValue);
    }
}

template <class theClass>
ctVector <theClass>::ctVector(unsigned int iLength) : length (iLength)
{
	data = new theClass*[iLength]();
	for (unsigned int i = 0; i < length; i++)
		data[i] = NULL;
}

//*******************Destructor*********************************


template <class theClass>
ctVector <theClass>::~ctVector()
{
	for (unsigned int i = 0; i < length; i++)
	{
		if (data[i] != NULL) delete data[i];
	}
	delete[] data;
}

//*************************Operators*******************************


template <class theClass>
theClass* ctVector <theClass>::operator[](unsigned int index)
{
	if (index >= 0 && index < length && data[index])
		return data[index];
	return NULL;
}

template <class theClass>
theClass& ctVector <theClass>::operator=(theClass info)
{
	

	for (unsigned int i = 0; i < length; i++)
	{
		if (data[i] != NULL) delete data[i];
	}
	delete[] data;

	length = info.length;

	data = new theClass*[info.length];

	for (i = 0; i < info.length; i++)
	{
		data[i] = new theClass(info[i]);
	}
	return *this;
}

template <class theClass>
theClass& ctVector <theClass>::operator=(theClass* info)
{
	

	for (unsigned int i = 0; i < length; i++)
	{
		if (data[i] != NULL) delete data[i];
	}
	delete[] data;

	length = info->length;

	data = new theClass*[info->length];

	for (i = 0; i < info->length; i++)
	{
		data[i] = new theClass((*info)[i]);
	}
	return *this;
}

//***********************Accessors*************************************


template <class theClass>
theClass* ctVector <theClass>::get(unsigned int index)
{
	if (index >= 0 || index < length)
		return data[index];
	return NULL;
}

template <class theClass>
void ctVector <theClass>::put(unsigned int index, theClass* info)
{
	if (index < 0 || index > length)
		return;
	if(data[index])
		delete data[index];
	data[index] = info;
}

template <class theClass>
void ctVector <theClass>::put(unsigned int index, theClass info)
{
	if (index < 0 || index >= length)
		return;
	if(data[index])
		delete data[index];
	data[index] = new theClass(&info);
}

template <class theClass>
void ctVector <theClass>::remove(unsigned int index)
{
	if (index >= 0 && index < length && data[index])
	{
		delete data[index];
		data[index] = NULL;
	}
}

template <class theClass>
unsigned int ctVector <theClass>::getLength(){return length;}

#endif

;
  
Ok, thanks for the help all! A frusterated cheeZe gOd

Share this post


Link to post
Share on other sites