Jump to content
  • Advertisement

Archived

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

cheeZe gOd

defunct .cpp file with templates

This topic is 5989 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

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!