tags:

views:

55

answers:

2

Hey all, so this seems to be a rather strange issue to me. I have a very simple templatized container class which is part of a DLL. The entire class is defined in a header file, to allow automatic template generation. Now another part of the DLL actually requests a certain template type to be generated, so the code should exist within the DLL. However, when using the object from another executable, the constructor/destructor, plus multiple other functions work, however 2 functions are not found by the linker. Following are is the code for these two functions, as well as for a working function.

const T** getData() const
{
 return m_data;
}

int getNumRows() const
{
 return m_nRows;
}

int getNumCols() const
{
 return m_nCols;
}

So the getNumRows() and getNumCols() functions are not found by the linker, however the getData() function is. Is this a common problem, do the functions need to have a templatized parameter in order to be generated?

@1 800 INFORMATION

I have exported this from the DLL via a standard macro:

#ifdef ACORE_EXPORTS
#define ACORE_API __declspec(dllexport)
#else
#define ACORE_API __declspec(dllimport)
#endif

And at the class definition:

template < class T >
class ACORE_API matrix
+1  A: 

Are you actually exporting the functions from the library? You would mention the names in the .def file, or use the dllexport and dllimport directives in order to accomplish this.

1800 INFORMATION
Yes I am, I would imagine that would be answered by the fact that some functions work. But yes I am using a dllexport/dllimport macro for this class.
+6  A: 

The compiler will only generate member functions that are actually called.

For instance:

template <class T>
class MyClass
{public:
    int function1()
    {
     return 0;
    }
    int function2()
    {
     T t;
     t->DoSomething();
     return 0;
    }
};

and then later

MyClass<int> m;
m.function1();

compiles, because MyClass::function2() was never compiled.

You can force the instantiation of the entire class by doing this:

template class MyClass<int>;

In that case, every method in the class will be instantiated. In this example, you get a compiler error.

Johan Ericsson
Thank you Johan, it seems very simple now hehe. I was calling the one (working) function within the DLL and it was thus generated, however not the other (non-working) functions. I was under the impression that something like MyClass<int> m; at any point, would cause the entire class to be generated for int.
Thanks... cool, this was my first time using this site. It is really well put together!
Johan Ericsson