在类库项目C ++中创建模板类

时间:2021-11-24 17:00:14

I have a C++ class library project that is commonly used by other C++ projects. To be able to use classes inside my class library project, I wrote a header file like the example given below

我有一个C ++类库项目,它通常被其他C ++项目使用。为了能够在我的类库项目中使用类,我编写了一个头文件,如下面给出的示例

#pragma once
#ifdef MYLIB
# define MYLIB_EXPORT __declspec(dllexport)
#else
# define MYLIB_EXPORT __declspec(dllimport)
#endif

No problem until I want to create a template class inside my class library project. The problem is I can't export my template class.

没问题,直到我想在我的类库项目中创建一个模板类。问题是我无法导出我的模板类。

MyClass.h

template<class T>
class MYLIB_EXPORT MyClass
{
    void myMethod();
    // ...
}

template<class T>
void MyClass::myMethod()
{
    // ...
}

In this case I am getting compilaton errors saying "definition of dllimport function not allowed". I know what causes the problem and I understand it. Other projects using my class library project converts the MYLIB_EXPORT keyword to __declspec(dllimport). Therefore, they are expecting the methods of MyClass to be defined in a DLL. But, then the compiler sees the definition inside the header.

在这种情况下,我得到compilaton错误,说“不允许定义dllimport函数”。我知道是什么导致了这个问题,我明白了。使用我的类库项目的其他项目将MYLIB_EXPORT关键字转换为__declspec(dllimport)。因此,他们期望在DLL中定义MyClass的方法。但是,编译器会在标头内看到定义。

How can I overcome this situation and be able to export my template classes that are defined inside my class library project?

如何克服这种情况并能够导出我的类库项目中定义的模板类?

2 个解决方案

#1


4  

Uninstantiated templates can't be compiled directly - they are code generators, so they are actually translated to binary instructions only when they are instantiated; for this reason, you can't export a template "in binary form" as if it was a "regular" function/class (on the other hand, at least in theory you could export an instantiation of a template).

未实例化的模板无法直接编译 - 它们是代码生成器,因此它们实际上只有在实例化时才会转换为二进制指令;因此,您无法以“二进制形式”导出模板,就好像它是一个“常规”函数/类(另一方面,至少在理论上您可以导出模板的实例化)。

Long story short: just leave the templates in a header to be included by library clients.

简而言之:只需将模板保留在库客户端包含的标题中即可。

Notice that this is the exact reason why you keep templates in headers and you don't normally separate their implementation in .cpp files.

请注意,这就是您将模板保留在标头中的确切原因,并且通常不会在.cpp文件中分隔它们的实现。

#2


0  

Just remove the MYLIB_EXPORT statement on template class. Then you can define class functions outside the class (but still in *.h or *.hpp header files).

只需删除模板类中的MYLIB_EXPORT语句即可。然后,您可以在类外定义类函数(但仍然在* .h或* .hpp头文件中)。

MyClass.h

    template <typename T>
    class MyClass    // MYLIB_EXPORT removed
    {
        void myMethod();
        // ...
    };

    template <typename T>
    void MyClass<T>::myMethod()
    {
        // ...
    }

I got this issue. After a long time, I realized removing MYLIB_EXPORT fixed it. Hope this answer will save time to others :-)

我遇到了这个问题。很长一段时间后,我意识到删除了MYLIB_EXPORT修复它。希望这个答案可以为别人节省时间:-)

#1


4  

Uninstantiated templates can't be compiled directly - they are code generators, so they are actually translated to binary instructions only when they are instantiated; for this reason, you can't export a template "in binary form" as if it was a "regular" function/class (on the other hand, at least in theory you could export an instantiation of a template).

未实例化的模板无法直接编译 - 它们是代码生成器,因此它们实际上只有在实例化时才会转换为二进制指令;因此,您无法以“二进制形式”导出模板,就好像它是一个“常规”函数/类(另一方面,至少在理论上您可以导出模板的实例化)。

Long story short: just leave the templates in a header to be included by library clients.

简而言之:只需将模板保留在库客户端包含的标题中即可。

Notice that this is the exact reason why you keep templates in headers and you don't normally separate their implementation in .cpp files.

请注意,这就是您将模板保留在标头中的确切原因,并且通常不会在.cpp文件中分隔它们的实现。

#2


0  

Just remove the MYLIB_EXPORT statement on template class. Then you can define class functions outside the class (but still in *.h or *.hpp header files).

只需删除模板类中的MYLIB_EXPORT语句即可。然后,您可以在类外定义类函数(但仍然在* .h或* .hpp头文件中)。

MyClass.h

    template <typename T>
    class MyClass    // MYLIB_EXPORT removed
    {
        void myMethod();
        // ...
    };

    template <typename T>
    void MyClass<T>::myMethod()
    {
        // ...
    }

I got this issue. After a long time, I realized removing MYLIB_EXPORT fixed it. Hope this answer will save time to others :-)

我遇到了这个问题。很长一段时间后,我意识到删除了MYLIB_EXPORT修复它。希望这个答案可以为别人节省时间:-)