0

I am migrating my Visual Studio 2005 C++ code to Visual Studio 2010. Unfortunately I am getting error on std::string on VS2010 whereas in VS2005 I never had this error before.

Here is the code sample

#include<string>

typedef std::string String

class __declspec(dllexport) SomeClass
{
public:
   String somevariable;  // compiler warning here.  Please see below for the compiler warning.

   void SomeFunction(String sName);  // No compiler errors or warning here
};

Compiler warning as:

error C2220: warning treated as error - no 'object' file generated
warning C4251: 'SomeClass::somevariable' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'SomeClass'

with
[
    _Elem=char,
    _Traits=std::char_traits<char>,
    _Ax=std::allocator<char>
]

Please help to give me solution for this issue.

AbuHafsah
  • 31
  • 3
  • http://stackoverflow.com/questions/5661738/common-practice-in-dealing-with-warning-c4251-class-needs-to-have-dll-inter – Arun Mar 25 '13 at 03:35
  • 1
    A possible solution using PIMPL idiom is here -http://stackoverflow.com/questions/767579/exporting-classes-containing-std-objects-vector-map-etc-from-a-dll – Arun Mar 25 '13 at 03:40

1 Answers1

0

I'd suggest reading and understanding the link that Arun gave first. Then, if you can be reasonably sure that you are not using different compilers (which is a recipe for disaster in C++ anyway), you can simply disable/ignore the warning.

BTW:

  • I get the same warning from VS2005 for some code.
  • You are perfectly right that the behaviour is inconsistent. The function and the membervariable should either both trigger the diagnostic or none of them.
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55