std::string在VisualStudio2010中提供编译器警告 < >我将VisualStudio 2005 C++代码移植到VisualStudio 2010。不幸的是,我在VS2010的std::string上遇到了错误,而在VS2005中,我以前从未遇到过这个错误

std::string在VisualStudio2010中提供编译器警告 < >我将VisualStudio 2005 C++代码移植到VisualStudio 2010。不幸的是,我在VS2010的std::string上遇到了错误,而在VS2005中,我以前从未遇到过这个错误,c++,C++,下面是代码示例 #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 com

下面是代码示例

#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
};
#包括
typedef std::string字符串
类uu declspec(dllexport)SomeClass
{
公众:
String somevariable;//此处为编译器警告。有关编译器警告,请参见下面的内容。
void SomeFunction(字符串sName);//此处没有编译器错误或警告
};
编译器警告为:

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>
]
错误C2220:警告被视为错误-未生成“对象”文件
警告C4251:“SomeClass::somevariable”:类“std::basic_string”需要具有dll接口,才能由类“SomeClass”的客户端使用
具有
[
_Elem=char,
_Traits=std::char\u Traits,
_Ax=std::分配器
]

请帮助我解决这个问题。

我建议阅读并理解Arun首先给出的链接。然后,如果你可以合理地确定你没有使用不同的编译器(这是一个C++的灾难性的配方),你可以简单地禁用/忽略警告。 顺便说一句:

  • 对于某些代码,我从VS2005中得到了相同的警告
  • 你完全正确,这种行为前后不一致。函数和membervariable应同时触发诊断或不触发诊断

使用PIMPL习惯用法的可能解决方案如下-