C2491, Templates and DLLImport

Hello,

my project folder contains two projects. The first one includes a gbArray class which is responsible for array handling. This project is build as DLL. The second project uses that DLL and its implemented classes.

At first I had the following problem:
I've tried to make the class much more flexible by using templates. At first that does not work, I got a linker error, that means, I have to define template methods in my header file. At this point the methods I used as templates are defined in my header class and the declaration is in my cpp file. I searched for the right solution and on some websites I read, that I have to implement the template method declaration also in my header file.

After this steps I got a new error:
1
2
3
gbarray.h(64): error C2491: 'gbArray<T>::gbArray': Definition von Funktion für dllimport nicht zulässig 
gbarray.h(77): error C2491: 'gbArray<T>::~gbArray': Definition von Funktion für dllimport nicht zulässig 
gbarray.h(89): error C2491: 'gbArray<T>::operator []': Definition von Funktion für dllimport nicht zulässig


My gbArray class looks as the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template <class T> 
class MY_API gbArray 
{ 
    protected: 
    private: 
        //  Variables 
        T * mArray; 
        int mRows, mCols; 
        
    public: 
        //  (De-)Constructor & Copy 
        gbArray(int pRows, int pCols); 
        ~gbArray(); 
  
        //  Operators 
        T * operator[](int pIndex); 
};

template <class T> 
gbArray<T> :: gbArray(int pRows, int pCols) 
{ 
    ... 
} 
  
template <class T> 
gbArray<T> :: ~gbArray() 
{ 
    ... 
} 
  
template <class T> 
T * gbArray<T> :: operator[](int pRow) 
{ 
    ... 
}


MSDN means:
 
Data, static data members, and functions can be declared as dllimports but not defined as dllimports. 


I'm not realy sure about that message, but does it mean I cannot declare DLLImport in my header file?

That is the define tag of my MY_API token:
1
2
3
4
5
#ifdef MY_EXPORTS
	#define MY_API __declspec(dllexport)
#else
	#define MY_API __declspec(dllimport)
#endif 


Do you have any ideas about that problem?

Best regards,
SKiD.
In fact, dllimport means you're importing your data from a DLL (For this reason, you cannot provide your own function bodies as they will be taken from DLLs).

You should avoid importing and exporting template classes anyways, because it's impossible to do so as they're calculated on a per-instance basis.
Last edited on
Thank you for your answer!

So I have to implement a gbArray class for every datatype I need? There is no other way?
So I have to implement a gbArray class for every datatype I need? There is no other way?
No, why not using that template. It's header only, so no dll is required. I'd say that's a plus.

Is there any reason why you want the dll?
Argh, I'm an Idiot. You're right!
I only saw the DLL, not the header file!
I include the header file and the problem is gone. Thank you! (;
Topic archived. No new replies allowed.