Archived

This topic is now archived and is closed to further replies.

Quick question about template parameters

This topic is 5969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, Here's a simplified version of my template class. It's my first ever one and it's giving me stick: template < T > class MY_CLASS { public: MY_CLASS( void ) {} // Default constructor. ~MY_CLASS( void ) {} // Destructor. inline T *GetData( void ) // Returns a pointer to the node's data. { return m_pData; } private: T *m_pData; // Pointer to the node's data. }; And here are the first couple of errors I'm getting: error C2061: syntax error : identifier 'T' error C2992: 'MY_CLASS' : invalid or missing template parameter list see declaration of 'MY_CLASS' So basically it looks like VC++ is disagreeing with my template name. Does anyone have any idea why this might be happening??? Thanks! Gareth Edited by - Obolus on August 11, 2001 9:48:52 AM

Share this post


Link to post
Share on other sites
The reason VC is complaining is that it likes statements to be syntactically correct, which your declaration is not.

It should probably be:
  
template <class T>
class MY_CLASS {
// Stuff...


};


Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
Right, problem no. 2 :

I have a class which stores a pointer to a templated object. I want the normal class to set its pointer to point to any instance of that template, e.g.:


template
class TemplatedClass
{
...
private:
T m_SomeData;
...
};


class NormalClass
{
private:
...
TemplatedClass< T > * m_Link;
...
public:
...
void SetData( TemplatedClass< T > * NewLink )
{ m_Link = NewLink; }
...
};


So, I create a TemplatedClass with, say, an ''int'' as the data type. The problem occurs when I try to set the pointer in the NormalClass to point to a specific TemplatedClass object, e.g.:


NormalClass normal;
TemplatedClass< int > *templated = new TemplatedClass< int >;

normal.SetData( templated );


VC++ produces an error at the "normal.SetData( templated );" line, saying:


error C2664: ''SetHead'' : cannot convert parameter 1 from ''class TemplatedClass< int > *'' to ''class TemplatedClass< T > *''


So it seems to me that you can''t pass templated objects as member function parameters. Is there a solution to this problem, or was I simply over-estimating the usefulness of a template?

Thanks again,
Gareth

Share this post


Link to post
Share on other sites