Template classes
-
template<class _T>
_T TCollection**<_T>**::at(unsigned int iPos){...}template<class _T>
void TCollection**<_T>**::add(_T T) {...}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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.
-
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 ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
template classes inmplementations have to be in the header... what is the problem with pData exactly ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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.
-
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:
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!
-
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?When defining you must explicitly declare the function as template:
template <class _T> TCollection<_T>::TCollection () : pData(0), pCount(0) { } template <class _T> TCollection<_T>::~TCollection () { clear(); }
---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.
-
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:
template <class _T> TCollection<_T>::TCollection () : pData(0), pCount(0) { } template <class _T> TCollection<_T>::~TCollection () { clear(); }
---------------------- 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...)
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
template classes inmplementations have to be in the header... what is the problem with pData exactly ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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
-
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...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
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?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.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ