For Programmers: Free Programming Magazines  


Home > Archive > Visual Studio > September 2004 > Creating Dynamic Arrays of a Type Passed as a Template Parameter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Creating Dynamic Arrays of a Type Passed as a Template Parameter
Artist

2004-09-20, 4:02 am

I am trying to create within a class a class member that is a dynamic
array of a type passed as a template argument. The esential code
fragments are below. I tried both class and typename keywords and for
each of them I get a different error message.

When I attempt the class keyword I get the error:
cDicastArray.cpp(28) : error C2512: 'T' : no appropriate default
constructor available

I did not know how to specify a constructor for class passed as a
template argument. I do not see how the compiler can require this.
Requiring it would seem to defeat the purpose of templates. Why does it
do this? In the hope that the typename keyword would be treated
differently I tried it and got the error:

cDicastArray.cpp(5) : error C2065: 'T' : undeclared identifier

Why did not the typename keyword declare the T parameter?


**** class Attempt ****

template< class T > class cDicastArray
{ private:
unsigned int cA;

public:
T *A;
}

cDicastArray<class T>::cDicastArray( unsigned int N, ... )
{
// cDicastArray.cpp(28) : error C2512: 'T' : no appropriate default
// constructor available in the following line:
A = new T[cA];
}

**** typename Attempt ****

template< typename T > class cDicastArray
{ private:
unsigned int *D;
}

// cDicastArray.cpp(5) : error C2065: 'T' : undeclared
// identifier next line:
unsigned int cDicastArray<typename T>::getD( unsigned int i )
{ return D[i];
}

--
If sj. appears in my email address remove it to respond.
It is a spam jammer.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com