• Advertisement

Archived

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

initializing template pointers.....

This topic is 6072 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

been havin troubles creating a pointer to a templated class..
  

template <class T>
class myClass
{
public:
     myClass();
     virtual ~myClass();

     virtual void foo();
};

template <class T>
myClass<T>::myClass(){}

template <class T>
myClass<T>::~myClass(){}

template <class T>
myClass<T>::foo(){}

//I then insert the following code somewhere..

myClass<int>* temp = new myClass();
  
is this right? Can''t seem to find a way for it to work, I''ve tried every syntax combination but this seems the most natural.. I''m using VC++, MSDN is no help at all...

Share this post


Link to post
Share on other sites
Advertisement
I''ve also tried this

  
//not a pointer

myClass<int> temp;


it keeps complaining about linking, when I have a pointer, it complians that the type is not specified, when I don''t do it as a pointer, it says it can''t find default constructor....

weird stuff...

Share this post


Link to post
Share on other sites
For dynamic creation try this:
myClass* temp = new myClass<int>();
(Maybe this would also do:
myClass* temp = new myClass<int>;)

baumep

Edited by - baumep on September 7, 2001 6:09:46 AM

Share this post


Link to post
Share on other sites
You're nearly there... try this..

      

template <class T>
class myClass
{
public:
myClass();
virtual ~myClass();
virtual void foo();
};
template <class T>
myClass<T>::myClass(){}
template <class T>
myClass<T>::~myClass(){}
template <class T>
void myClass<T>::foo(){}

//I then insert the following code somewhere..


myClass<int>* temp = new myClass<int>();



Edited by - Sandman on September 7, 2001 6:16:09 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
both don''t work, its having linking errors..... DAMN VC++!!!

keeps saying un-resolved external symbol, but I have everything coded up exactly what I have written above.. I tried what you guys suggested but still dosn''t work.... weird...

Share this post


Link to post
Share on other sites
What are the linker errors?

Do you define the template functions in a different file to the declarations? Try putting them all in the same file, see if that helps.

eg.

  
// in MyClass.h


template <class T>
class myClass
{
public:
myClass();
virtual ~myClass();
virtual void foo();
};
template <class T>
myClass<T>::myClass(){}
template <class T>
myClass<T>::~myClass(){}
template <class T>
void myClass<T>::foo(){}

// in main.cpp


//I then insert the following code somewhere..

myClass<int>* temp = new myClass<int>();

Share this post


Link to post
Share on other sites
I also had linker errors when separating my template class in a header file and a cpp file. You should try what Sandman suggested and see if there are still linker errors. (May be it is not an error with your template class which seems to be correct but with VC++''s buggy imlementation of templates)

baumep

Share this post


Link to post
Share on other sites
Thanks everyone! I got home, check the suggestions and I put everything into one header file. It worked! But I''m very dissapointed in VC++, such a widely used compiler and its trivial flaw(s)...

Share this post


Link to post
Share on other sites
Don''t judge MSVC too quickly... it has flaws, but this problem is not really a significant one of them. Due to the nature of templates, they should all be in a header file. This is because templates aren''t code like normal functions are: rather, they are instructions for how to produce code. If those instructions are in abc.cpp. and code that needs those instructions is in xyz.cpp, there''s no practical way to achieve this, because cpp files are totally separate at compile time. And there''s no way of fixing it at link time because the instructions in abc.cpp haven''t been instantiated into object code for the type you used in xyz.cpp.

Some (very few) compilers support the ''export'' keyword which gets around this, but it''s not exactly trivial to implement. The ''templates in header files'' limitation makes a lot of sense.

Share this post


Link to post
Share on other sites
I wouldn''t consider that a flaw in MSVC either...
I put my templates in .hpp files, I''m not sure if there''s a convention or not - but putting them in .cpp files is uncharacteristic.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites

  • Advertisement