Advertisement

defunct .cpp file with templates

Started by June 26, 2002 05:56 PM
2 comments, last by cheeZe gOd 22 years, 5 months ago
Ok, I''ve been stuck on this for a couple of weeks. I''ve made my own safe array class (who hasen''t?) but I get linker errors if I put the function body in a .cpp file instead of the .h file. Here''s the code. ctVector.h
  
#ifndef _CTVECTOR_
#define _CTVECTOR_
#include <memory.h>

template <class theClass> 
class ctVector
{
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(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

;

  
ctVector.cpp
  
#include "ctVector.h"

//just one function in the .cpp to demonstrate the error

template <class theClass>
ctVector <theClass>::ctVector() : length(10)
{
	data = new theClass*[length]();
	for (unsigned int i = 0; i < length; i++)
		data[i] = NULL;
}
  
main.cpp
  
#include <stdio.h>
#include "ctVector.h"

void main()
{
	ctVector<int>();
}
  
main.obj : error LNK2001: unresolved external symbol "public: __thiscall ctVector::ctVector(void)" (??0?$ctVector@H@@QAE@XZ) I avoided this for a while (just have large .h files) but now it''s a bottle neck on my coding. Thanks for the help! Oh, and if it looks sloppy, tell me how to make it more readable to other programmers. A bamboozled, cheeZe gOd
cheeZe gOd
See there.
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
Or even better (ok, I may be a bit biased for this thread ), see here.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 27, 2002 1:14:12 AM]
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Just to one you up, Chem0sh, see there

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement