Board index » Visual Studio » Template class in Managed C++

Template class in Managed C++

Visual Studio203
I have problem using template class in a managed C++ dll. I got error C2065:

'MyTempateCall' : undeclared identifier. I don't have problme use un-template

class.

Below is the simple example it has the same problem as my real project.



// The dll library

#pragma once

using namespace System;

namespace TestDll {



public ref class Class3

{

};



template<class T>

public ref class Class2

{

public:

Class2<T>(T theValue)

{

myValue = theValue;

}



private:

T myValue;

};



template ref class Class2<int>;

template ref class Class2<double>;





public ref class TestClassInSameNameSpace

{

void Test()

{

Class2<int>class2(60);

}



private:

Class3 class3;

};

}



// The Other application that uses the privious dll. Now I got this error:

error

// C2065: 'Class2' : undeclared identifier. It is fine with Class3.

#pragma once

using namespace System;

using namespace TestDll;



namespace CallTestDLL {



public ref class TestClass

{

void Test()

{

Class2<int>class2(60);

}



private:

Class3 class3;

};

}



I add the dll as the reference. I got same error message if I add the

library project as the reference.

What did I miss?

Thank you for any help.


-
 

Re:Template class in Managed C++

Haiping wrote:

Quote
[...]

I add the dll as the reference. I got same error message if I add the

library project as the reference.

What did I miss?



Templates can't be exported over dll boundaries easily, it's rather

tricky to do it and only with the restriction to export defined template

specializations.



Quote
Thank you for any help.



Why don't you use generics ? At least for this "simple" case it's simple

to replace the template class by a generic one.



E.g. rewrite your example as:



generic<typename T>

public ref class Class2

{

public:

Class2<T>(T theValue)

{

myValue = theValue;

}



private:

T myValue;

};





And you can use this class not only from C++, but any .NET 2.0

compatible language.

The "downside" is that not all C++ template classes can be replaced by

generic ones. But if you want to export generic classes, IMHO generics

are the best way to go.



Hope this helps,

Andre

-

Re:Template class in Managed C++

Hi Andre,



This is just a example. In our realy project the Class2 is also derive from

a native C++ template class. So it cannot be generic.



I solved the problem by use the dependencies instead of reference, and also

include the .h and .cpp files.



Thanks for your response,

Haiping



-

Re:Template class in Managed C++

Haiping wrote:

Quote
Hi Andre,



This is just a example. In our realy project the Class2 is also derive from

a native C++ template class. So it cannot be generic.



Hm, but Class2 is a ref class, a managed one. If I haven't missed

something, managed classes cannot (yet) derive from native ones and vice

versa.

So I wonder how you accomplished that ?





Quote
I solved the problem by use the dependencies instead of reference, and also

include the .h and .cpp files.



Then you don't need a dll at all ;-)



Quote
Thanks for your response,

Haiping



:-) Your welcome. Though it didn't help that much. But you found a

solution of

your own.



Regards,

Andre





-