samedi 23 novembre 2019

Using template cass from DL/so/dyib

ALL,

I have following code inside my library:

//header
template<class T>
class __declspec(dllexport) MyClass : public MyBaseClass {
public:
    MyClass(T obj);
    ~MyClass();
};

//cpp file
template<class T>
__declspec(dllexport) MyClass<T>::MyClass(T obj) : MyBaseClass() {
    // body of the constructor
}

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

Now I'm trying to use this class.

Turns out I can't write:

// header
class Foo
{
private:
    MyClass<T> *obj;
};

// cpp file

void Foo::some_func()
{
    obj = new MyClass<Test>();
}

as I get the compilation error, but I also can't write:

// cpp file
void Foo::some_func()
{
    Test *test = new Test();
    MyClass *obj = new MyClass<Test>( test );
}

as I get a linker error.

So what is the proper way to use template class from the library?

TIA!!

Aucun commentaire:

Enregistrer un commentaire