initializing template pointers.....

Started by
8 comments, last by GOOSEDUMP 22 years, 7 months ago
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...
Advertisement
I''ve also tried this

  //not a pointermyClass<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...
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
baumep
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
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...
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.htemplate <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>();    

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
baumep
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)...
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.
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement