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