Problem with template class in Managed C++

H

Haiping

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 in the same dll.
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.
 
C

Carl Daniel [VC++ MVP]

Haiping said:
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 in the same dll.
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?

You missed that a template is not code, it has no address and it's not
exported from a DLL and does not appear in the CLR metadata. Managed
templates are not designed to be across assembly boundaries.

You have basically three choices:
1. Instantiate the template inside TestDLL. The instantiation is an ordinary
class and will appear in the CLR metadata.
2. Use a generic class instead of a template.
3. #include the definition of the template class in the module where you
want to use it.

Remember - templates are really just fancy macros: a source code mechanism
understood only by the C++ compiler.

-cd
 
B

Ben Voigt [C++ MVP]

Carl Daniel said:
You missed that a template is not code, it has no address and it's not
exported from a DLL and does not appear in the CLR metadata. Managed
templates are not designed to be across assembly boundaries.

You have basically three choices:
1. Instantiate the template inside TestDLL. The instantiation is an
ordinary class and will appear in the CLR metadata.
2. Use a generic class instead of a template.
3. #include the definition of the template class in the module where you
want to use it.

Furthermore, that is C++/CLI, not managed C++. Managed C++ died with
VS2003, C++/CLI is supported by VS2005 and VS2008 and into the future.
 
H

Haiping

Thank you Carl and Ben,

I will try the 3 options and let you know how it is going.

Haiping
 
H

Haiping

Hi Carl,

I have questions about the 3 options:
1. Instantiate the template inside TestDLL. The instantiation is an ordinary
class and will appear in the CLR metadata.
---- I thought the following lines are the instantiations of the template
class. If they are not what is the right wasy to instantiate the template?
template ref class Class2<int>;
template ref class Class2 said:
2. Use a generic class instead of a template.
----- Our template class is derived from a template class which is in a
native C++ library. Can a generic class to be derived from a template class?
3. #include the definition of the template class in the module where you
want to use it.
---- Do you mean include the .h and .cpp files? I tried that. I got "class
type redefinition" error. I think because I added the C++/CLI project as
reference project.
In my real project I also tried to add the C++/CLI project as a
depencency not a reference. In that case I don't get "class type
redefinition" error. But I got LNK error. I have some classes that implement
the C# interfaces. I don't know if this is the reason that causes the LNK
error.

Thank you,
Haiping
 
H

Haiping

Hi Carl,
3. #include the definition of the template class in the module where you
want to use it.
--- This way works for us. We need inclued the .h and the .cpp files

Thank you again for your help.
Haiping
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top