PDA

View Full Version : warning C4251:



vermarajeev
31st October 2006, 11:11
Hi guys,
I'm creating a dll for my project, but having problem with
this famous warning

warning C4251: 'std::_Vector_val<_Ty,_Alloc>::_Alval' : class 'std::allocator<_Ty>'
needs to have dll-interface to be used by clients of class 'std::_Vector_val<_Ty,_Alloc>'

I had a good look at google and found some solutions which were quite satifying


class COMMON_DLL_API ConnectionTable
{
public:
struct CtRow
{

};

};
For the above class I used the below code and it works fine

template class COMMON_DLL_API std::allocator<CtRow*>;
template class COMMON_DLL_API std::vector<CtRow*, std::allocator<CtRow*> >;
std::vector<CtRow*> table;
Now my problem is with this code

template<class T> class COMMON_DLL_API SynsupVector : public std::vector<T>
{

};For the above code I still get the same warnings. I tried using the same above
syntaxs but get compile time errors if done so.

Any help will be highly appreciated

vermarajeev
2nd November 2006, 13:44
Hi guys,
Come on!!! provide some input please, I'm getting nightmare coz it doesnt allow me run my module.

Help me I'm in a great problem.

jacek
2nd November 2006, 21:01
provide some input please, I'm getting nightmare coz it doesnt allow me run my module.
Well... it's not that easy. Templated code exists only in potentia until you instantiate the template either by using it, or like this:

template class COMMON_DLL_API std::allocator<CtRow*>;
template class COMMON_DLL_API std::vector<CtRow*, std::allocator<CtRow*> >;

That SynsupVector class is a template, so there is no physical code that you could put in a DLL and use it later. What you could try is putting all of the code in header files, but without COMMON_DLL_API. Like this:
template<class T> class SynsupVector : public std::vector<T>
{
...
};
This way every application will have its own template instances and you will loose all of the benefits of shared libraries*, but at least it should work.


* You could try to delegate a part of the implementation to a non-template class and place that class in DLL.

stinos
3rd November 2006, 10:10
Now my problem is with this code

template<class T> class COMMON_DLL_API SynsupVector : public std::vector<T>
{

};

You should take care with this also: std::vector does not have a virtual destructor, it's not really meant to be derived from; if you would delete a SynsupVector*, vector's destructor won't get called, which can turn out quite problematic..
If you need to add functionality you're better of writing void myFunc( std::vector<T>& ) or using the vector as a member variable.
Of course that doesn't solve the dll export problem since a template isn't compiled completely unless you use it.
Interesting problem ;-]