【问题标题】:std::string gives compiler warning in Visual Studio 2010std::string 在 Visual Studio 2010 中给出编译器警告
【发布时间】:2013-03-25 03:29:14
【问题描述】:

我正在将我的 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>
]

请帮我解决这个问题。

【问题讨论】:

标签: c++


【解决方案1】:

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

顺便说一句:

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

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多