Solved

dll-exporting a class defined with a template

Posted on 1998-09-13
3
231 Views
Last Modified: 2010-04-02
Hi,
The following code attempts to dll-export the class cBig; the MicroSoft Visual C++ 5.0 compiler generates the following warning:

warning C4251: 'mRange' : class 'TRange<long>' needs to have dll-interface to be used by clients of class 'CBig'

the code :
// Start of code
#define DllExport __declspec( dllexport )

template<class T> class TRange
{
.
.
}

class DllExport cBig
{
public:
.
.
protected:
    typedef TRange<long> CRange;
    CRange mRange;
}
// End of code

What should I do in order to resolve this warning? I've tried to insert an additional 'DllExport' almost everywhere in the code.
0
Comment
Question by:gil_mo
3 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1172584
I declared the template class as DllExport and added ";" to the ends of the classes like

template<class T> class DllExport TRange
   {
   } ;

   class DllExport cBig
   {
   public:
   protected:
       typedef TRange<long> CRange;
       CRange mRange;
   } ;

and it worked fine.
0
 

Author Comment

by:gil_mo
ID: 1172585
nietod,
warning persists if the class template isn't empty, e.g.

template<class T> class DllExport TRange
{
private:
    long a;
};

0
 
LVL 3

Accepted Solution

by:
xyu earned 50 total points
ID: 1172586
try to add this line before declaration of cBig

.
template class __declspec(dllexport) TRange<long>;
.
or
template<> class __declspec(dllexport) TRange<long>;

 it will help if TRange doesn't have outline implementation of the member functions like
template <class T> void TRange<T>::Foo() {...}

otherwise You'll receive on such a function warning like that..
"..dllexport assumed by default.." but its Ok... and You can live with it :) without any problem...

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question