|
I have defined a template class in a header file like so:
template <class _T> class TCollection {
public:
TCollection();
~TCollection();
Then try to declare the members in the cpp file like so:
TCollection::TCollection() : pData(0) , pCount(0)
{
}
TCollection::~TCollection()
{
clear();
}
Yet I am getting a compiler error
'TCollection' : use of class template requires template argument list
Obviously it cannot be treated like a normal class, so what is the correct way of declaring the members?
|
|
|
|
|
waldermort wrote: template <class _t=""> class TCollection
BTW, what is pData declared like ?
|
|
|
|
|
TCollection<class _T>::TCollection()
That worked, thankyou.
|
|
|
|
|
I tell a lie, it worked for the c'tor and d'tor but not the other members.
This is the header file:
template <class _T> class TCollection {
public:
TCollection();
~TCollection();
_T at(unsigned int iPos);
void add(_T T);
void clear();
int size();
bool empty();
private:
_T* pData;
int pCount;
};
And the cpp file looks something like this:
TCollection<class _T>::TCollection() : pData(0) , pCount(0)
{
}
TCollection<class _T>::~TCollection()
{
clear();
}
_T TCollection<class _T>::at(unsigned int iPos)
{
if (iPos >= 0 && iPos > pCount)
return pData[iPos];
else
return NULL;
}
void TCollection<class _T>::add(_T T)
{
if (pData) {
_T *tmp = new _T [pCount+1];
for (int i=0;i<pCount;i++)
tmp[i] = pData[i];
tmp[i] = T;
delete [] pData;
pData = tmp;
pCount++;
} else {
pData = new _T [1];
pData[0] = T;
pCount++;
}
}
I'm getting error like
E:\Program\foo\TCollection.cpp(18) : error C2027: use of undefined type '_T'<br />
E:\Program\foo\TCollection.cpp(8) : see declaration of '_T'<br />
E:\Program\foo\TCollection.cpp(20) : error C2036: 'class _T *' : unknown size<br />
E:\Program\foo\TCollection.cpp(28) : error C2512: '_T' : no appropriate default constructor available
This is only after a split it into seperate files, it worked in a single file.
|
|
|
|
|
template<class _T>
_T TCollection::at(unsigned int iPos){...}
template<class _T>
void TCollection::add(_T T){...}
|
|
|
|
|
Thanks for your help with this, but I am still getting errors.
template<class _T> _T TCollection::at(unsigned int iPos)
{
...
}
template<class _T> void TCollection::add(_T T)
Results in the first error:
E:\Program\foo\TCollection.cpp(17) : error C2955: 'TCollection' : use of class template requires template argument list<br />
e:\program\foo\tcollection.h(24) : see declaration of 'TCollection'<br />
E:\Program\foo\TCollection.cpp(25) : error C2955: 'TCollection' : use of class template requires template argument list
|
|
|
|
|
template<class _T>
_T TCollection<_T>::at(unsigned int iPos){...}
template<class _T>
void TCollection<_T>::add(_T T) {...}
|
|
|
|
|
Thankyou that worked, but now having problems with the pData pointer. I thought it would be easy to use seperate files. Rather than messing about I have placed the whole thing in the single header file, it's only a small class.
|
|
|
|
|
template classes inmplementations have to be in the header...
what is the problem with pData exactly ?
|
|
|
|
|
I can't remember eaxactly after putting it back in the header, it was something about an unknown class size when trying to delete it. Basicaly this is just a template similar to the vector class, with the exception it's smaller and calls the destructor.
|
|
|
|
|
toxcct wrote: template classes inmplementations have to be in the header.
This is only because there is no compiler out there that meets this part of the standard (mainly due to the insane complexity it takes to accomplish it). The standard allows for placing template definitions in header files and implementations in cpp files.
To get around this problem, I generally either put the entire class definition and implementation into a single file, or I place a #include "myclass.cpp" at the bottom of my header file.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
yes i know that, but that fact is that actually, so i didn't want to pollute the post with something that wouldn't have helped...
|
|
|
|
|
Probably problem is you have used TCollection class object without using argument list.
You need to use it like this,
TCollection<int> objTCollection;
|
|
|
|
|
waldermort wrote: I have defined a template class in a header file like so:
It's a class template, not a template class.
waldermort wrote: Then try to declare the members in the cpp file like so:
You cannot define template members in a cpp file. Look up the C++ FAQ!
|
|
|
|
|
Well I do appologise if my English offends you, although it appears the other members (forum members that is and not class members before you get confused) here did not have such a hard time understanding me. May I ask, Mr.Anonymuos, do you use the same attitude when your customers come to you with a problem?
|
|
|
|
|
When defining you must explicitly declare the function as template:
<br />
template <class _T> TCollection<_T>::TCollection () : pData(0), pCount(0)<br />
{<br />
}<br />
template <class _T> TCollection<_T>::~TCollection ()<br />
{<br />
clear();<br />
}<br />
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
well, you're right.
indeed, i think i already answered him (if you read the previous posts...)
|
|
|
|
|
Separating template classes into .h and .cpp files is too much of a headache when you're just starting out with templates. It's far easier to put everything in the .h file. See the C++ FAQ Lite[^] template section for more details.
|
|
|
|
|
Hi!
I have a question about dynamically allocated memory, actually about cleaning that memory.
For example I compile and run this piece of code:
char *pString = NULL;
if ( (pString = new char [100]) == NULL )
exit( EXIT_FAILURE );
... <--- Use pString
delete [] pString;
The question is how does the program know how much memory (how many bytes) to free (deallocate)? Isn't it compulsory to specify the number of bytes between the two brackets in order to be sure that it will free all the dynamically allocated memory?
Any suggestions would be greatly appreciated!
Thanks in advance!
|
|
|
|
|
Each heap allocation keeps track of its size.
Steve
|
|
|
|
|
|
|
Did you read his question ??
The link you posted has nothing to do with new and delete, it is purely C programming (alloc, malloc, realloc, ...).
When posting a link, at least take the time to verify if it contains valuable information for the OP.
Just googling and pasting the first link that cames up is not really helpfull.
|
|
|
|
|
It seems I must read complete a thread not only title,thanks for remind,sometime when I see a title I guess body is like title (thanks), but i dont think that stuff and paste link is good,yes?
|
|
|
|
|
WhiteSky wrote: It seems I must read complete a thread not only title
Of course !! That seems logical no ? How can you describe completely a problem in one single title line ??
WhiteSky wrote: but i dont think that stuff and paste link is good
It depends. Sometimes it is usefull, but sometimes it can be really annoying. For example if that links to a lot of text and if the OP needs to search a lot to find the information, it is in general not very valuable. And if you provide a link to something you googled for, then at least verify that it contains the information.
We ask the people who post the questions to have some respect for the other (so formulate the questions correctly, use proper english if possible, ...) but the opposite is true also.
|
|
|
|