Template classes
-
waldermort wrote:
template class TCollection
BTW, what is pData declared like ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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' E:\Program\foo\TCollection.cpp(8) : see declaration of '_T' E:\Program\foo\TCollection.cpp(20) : error C2036: 'class _T *' : unknown size 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. -
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' E:\Program\foo\TCollection.cpp(8) : see declaration of '_T' E:\Program\foo\TCollection.cpp(20) : error C2036: 'class _T *' : unknown size 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){...}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
template<class _T>
_T TCollection::at(unsigned int iPos){...}template<class _T>
void TCollection::add(_T T){...}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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 e:\program\foo\tcollection.h(24) : see declaration of 'TCollection' E:\Program\foo\TCollection.cpp(25) : error C2955: 'TCollection' : use of class template requires template argument list
-
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 e:\program\foo\tcollection.h(24) : see declaration of 'TCollection' 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) {...}
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
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