Advertisement

overloaded friend operator= with templates - compiler errors

Started by August 03, 2002 12:04 PM
10 comments, last by cheeZe gOd 22 years, 4 months ago
Thanks SabreMan and Sneftel, but, alas, even with those modifications I''m still not getting any discernable function to call.

Here''s my total code (since SabreMan mentioned I never posted it.)


  //********ctBag.h**********#ifndef _CTBAG_#define _CTBAG_#include <iostream.h>template <class theClass>class ctBag{public:	ctBag();	ctBag(unsigned int iLength);	ctBag(const ctBag& bag); //just modified this because of advice by Sneftel	ctBag(unsigned int iLength, theClass fillValue);	ctBag(unsigned int iLength, theClass* fillValue);	~ctBag();	unsigned int getLength();	theClass* operator[](unsigned int index);	//just modified these because of advice by SabreMan	ctBag<theClass>& operator= (const theClass info);		//I just tried commenting this out but still		//got the same error	ctBag<theClass>& operator= (const 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);	void resize(unsigned int nextSize);protected:		theClass** data;	unsigned int length;};//*********************Constructors/Destructor***************************//copy constructor, just a dummy for nowtemplate <class theClass>ctBag<theClass>::ctBag(const ctBag& bag){	cout<<"ctBag(*ctBag)"<<endl;}template <class theClass>ctBag<theClass>::ctBag(unsigned int iLength, theClass* fillValue): length(iLength), data(new theClass*[iLength]){	cout<<"ctBag(length, *value)"<<endl;    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]){	cout<<"ctBag(length, value)"<<endl;    for (unsigned int i = 0; i < iLength; i++)    {        data[i] = new theClass(fillValue);    }}template <class theClass>ctBag <theClass>::ctBag() : length(128){	cout<<"ctBag()"<<endl;	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){	cout<<"ctBag(length)"<<endl;	data = new theClass*[iLength]();	for (unsigned int i = 0; i < length; i++)		data[i] = NULL;}template <class theClass>ctBag <theClass>::~ctBag(){	cout<<"~ctBag"<<endl;	for (unsigned int i = 0; i < length; i++)	{		if (data[i] != NULL) delete data[i]; //memory failure here when destructor called 2nd time	}	delete[] data;}//*************************Operators*******************************template <class theClass>theClass* ctBag <theClass>::operator[](unsigned int index){	cout<<"Hee!"<<endl;	if (index >= 0 && index < length && data[index])		return data[index];	return NULL;}//as mentioned above, I tried commenting this out as the source of the error, got same results though//just moded return type because of advice by SabreMantemplate <class theClass>ctBag<theClass>& ctBag <theClass>::operator=(theClass info){	cout<<"operator=(info)"<<endl;	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;}//modified because of SabreMan''s advicetemplate <class theClass>ctBag<theClass>& ctBag <theClass>::operator=(const theClass& info){	cout<<"Operator=(&info)"<<endl;	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* ctBag <theClass>::get(unsigned int index){	cout<<"Hee!"<<endl;	if (index >= 0 || index < length)		return data[index];	return NULL;}template <class theClass>void ctBag <theClass>::put(unsigned int index, theClass* info){	cout<<"Hee!"<<endl;	if (index < 0)		return;	if (index >= length)		resize (index * 5);	if (data[index])		delete data[index];	data[index] = info;}template <class theClass>void ctBag <theClass>::put(unsigned int index, theClass info){	cout<<"Hee!"<<endl;	put(index, new theClass(info));}template <class theClass>void ctBag <theClass>::remove(unsigned int index){	cout<<"Hee!"<<endl;	if (index >= 0 && index < length && data[index])	{		delete data[index];		data[index] = NULL;	}}template <class theClass>unsigned int ctBag <theClass>::getLength(){cout<<"Hee!"<<endl;return length;}//********************Private Functions********************template <class theClass>void ctBag <theClass>::resize(unsigned int nextSize){	cout<<"Hee!"<<endl;	if (nextSize <= length) return;	theClass** nextData = new theClass*[nextSize];	for (unsigned int i = 0; i < length; i++)		nextData[i] = data[i];	for (i = length; i < nextSize; i++)		nextData[i] = NULL;	delete data;	length = nextSize;	data = nextData;}#endif;//****main.cpp****void main(){	ctBag<int> bag	ctBag<int> bag2(20, 10);	bag = bag2;}//error box says "The instruction at ''0x00402acb'' refrenced memory at ''0xddddddd1''.  The memory could not be ''read''.  


The output is:
ctBag()
ctBag(length, value)
~ctBag
~ctBag

I''m going to see an old computer science teacher tomorrow (the 7th) and he''s gonna bring a fat book on templates and the answer may be in there.

I tried recompile all several times, just letting you all know.

Thank you sooooo much,

a frusterated, grateful, and confuzed,

cheeZe gOd
cheeZe gOd
Ok, I pinned it down and I feel like a moron (seems like the worst and most frusterating bugs make you feel like a moron).

I just changed

  template <class theClass>class ctBag{public:	ctBag<theClass>& operator= (theClass& info);};//to thistemplate <class theClass>class ctBag{public:	ctBag<theClass>& operator= (ctBag<theClass>& info);};  


and now I get a totally different set of errors, but I can deal with them.

Thanks again all!

cheeZe gOd
cheeZe gOd

This topic is closed to new replies.

Advertisement