2013-03-25 106 views
0

我正在将Visual Studio 2005 C++代码迁移到Visual Studio 2010.不幸的是,我在VS2010上遇到std :: string错误,而在VS2005中我从来没有发生过此错误。std :: string在Visual Studio 2010中给出了编译器警告

下面是代码示例

#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 
}; 

编译器警告为:

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> 
] 

请帮忙给我这个问题的解决方案。

+0

http://stackoverflow.com/questions/5661738/common-practice-in-dealing-with-warning-c4251-class-needs-to-have-dll-inter – Arun 2013-03-25 03:35:27

+1

使用PIMPL成语一种可能的解决方案是这里-http://stackoverflow.com/questions/767579/exporting-classes-containing-std-objects-vector-map-etc-from-a-dll – Arun 2013-03-25 03:40:07

回答

0

我建议阅读并理解Arun首先给出的链接。然后,如果您可以合理确信您没有使用不同的编译器(无论如何这都是C++的灾难处理方法),那么您可以简单地禁用/忽略该警告。

BTW:

  • 我从VS2005相同的警告对于一些代码。
  • 你是完全正确的行为是不一致的。函数和成员变量既可以触发诊断,也可以不触发它们。