std::string在Visual Studio 2010中给出编译器警告

std::string gives compiler warning in Visual Studio 2010

本文关键字:警告 编译器 Studio string Visual std 2010      更新时间:2023-10-16

我正在将Visual Studio 2005的c++代码迁移到Visual Studio 2010。不幸的是,我在VS2010上得到std::string错误,而在VS2005中,我以前从未有过此错误。

下面是代码示例
#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>
]

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

我建议先阅读并理解Arun给出的链接。然后,如果您可以合理地确定您没有使用不同的编译器(这在c++中无论如何都是导致灾难的原因),您可以简单地禁用/忽略警告。

顺便说一句:

  • 对于某些代码,我从VS2005中得到相同的警告。你完全正确,这种行为是不一致的。函数和成员变量要么都触发诊断,要么都不触发。