"definition of dllimport function not allowed" error

I am rebuilding my code (which was initially built using VC6 with no errors)
using .NET2005.
after fixing the code to get rid of the errors, I am still left with this
one.
I have simplified my code (see below) to reproduce the error:

error C2491: 'Array<T>::isIn' : definition of dllimport function not allowed

I looked up the MSDN help files but it didn't help much. can someone tell me
what I need to do to fix this
thanks,
Julian.

template <class T> class __declspec(dllimport) Array
{
private:
int numElements;
protected:
T **array;
public:
Array();
Array(int size, int inc=10);
~Array();
int isIn(const T &);
};

template <class T> int Array<T>::isIn(const T &t)
{
int i;
for(i=0;i<numElements;i++)
if(t==(*arrayIdea)) return i;
return -1;
}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}



Answer this question

"definition of dllimport function not allowed" error

  • Alagappapillai

    One more probable reason can be you may try to export the functions. But due to some switch definition combination or mistake if somebody uses __declspec(_dllimport) inplace of __declspec(dllexport) then this error comes.

    Probably solution is check whether you are importing/exporting the class from given source.

    Pl let me know still you have questions at girish.patel@rsystems.com



  • John Moore999

    You just need to get rid of the definition of the member function - not the definition of the class itself.

  • BPMIlls2001

    The error message is self explanatory. When you specify __declspec(dllimport), what you are telling the compiler is to import this class or function from elsewhere. So you cannot define a body for it. Are you sure you don't mean to use dllexport there

    Julian V wrote:

    I am rebuilding my code (which was initially built using VC6 with no errors)
    using .NET2005.
    after fixing the code to get rid of the errors, I am still left with this
    one.
    I have simplified my code (see below) to reproduce the error:

    error C2491: 'Array<T>::isIn' : definition of dllimport function not allowed

    I looked up the MSDN help files but it didn't help much. can someone tell me
    what I need to do to fix this
    thanks,
    Julian.

    template <class T> class __declspec(dllimport) Array
    {
    private:
    int numElements;
    protected:
    T **array;
    public:
    Array();
    Array(int size, int inc=10);
    ~Array();
    int isIn(const T &);
    };

    template <class T> int Array<T>::isIn(const T &t)
    {
    int i;
    for(i=0;i<numElements;i++)
    if(t==(*array)) return i;
    return -1;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    return 0;
    }



  • kabuki

    Hmm, I thought you couldn't compile template classes into an object before hand because it wouldn't have the functions for all the types.

    The stl library isn't compiled, it's all in the headers for the stl, like iostream and such. Since you will be shipping the entire template class around in the header there is no point in having the __declspec(dllimport) there so just get rid of it. You don't need it with template classes.

    The way you need to use a template class is to define it in one header file, all the methods need to be coded in the header file too. Once you have that then you include that header file in all code files which use your template class. You should find that it will then work.



  • Sianoosh

    I'm sorry I was not clear in my earlier post. I had simplified my original code so I could reproduce the error but I forgot to mention how the original program was structured. I have a dll that has a bunch of classes (including templates) and functions that are being exported. then I have this small (driving) program that uses this dll and imports all these functions. In the original version (which was compiled in VC6), I had provided the header files of all the imported functions when compiling the driving program.
    now when I try to build this in VC8, it is not letting me provide the header file for the template (Array).
    Instead of providing the header file, I just used:

    template <class T> class __declspec(dllimport) Array;

    but when I do this, I cannot use this template in the driver program.. it says:

    error C2079: 'test' uses undefined class 'Array<T>'

    I don't 'really' need to import this template as such into the driver program... but some of the classes that i do want to import uses this template as data members. and because of that it won't compile.
    I hope this is clearer now.. if not, please let me know and I will try to explain better.
    If this is not the appopriate way to design this, please let me know what is.
    can I split the template itself into a header and cpp file maybe I should try that
    thanks

  • "definition of dllimport function not allowed" error