c++ - 错误 C2797 : list initialization inside member initializer list

标签 c++ visual-studio inheritance visual-studio-2013

我在看MVA's tutorial on C++我在下面提到的代码是凯特而不是我写的。然而,她似乎在没有编译显示任何错误的情况下解决了它,但在我的情况下,我收到以下错误:

Error 1 error C2797: 'NamedRectangle::_name': list initialization inside member initializer list or non-static data member initializer is not implemented c:\users\abhimanyuaryan\documents\visual studio 2013\projects\kate demos\17 inheritance\inheritance\namedrectangle.h 12 1 Inheritance

代码中的第 12 行来 self 的 NameRectangle 类,它继承自 Rectangle 类:

class NamedRectangle :  public Rectangle
{
public:
    NamedRectangle() { }

    NamedRectangle(std::string initial_name, int initial_width, int initial_height)
        : Rectangle{ initial_width, initial_height }, _name{ initial_name } //--> This line
    {}  

std::string get_name() const { return _name; }

private:
    std::string _name;

};

当我从构造函数中删除 std::string initial_name 以及 _name{initial_name} 时,代码会编译。请解释我,不要把我当作一个更高标准的有经验的程序员。我昨天才开始学 C++。

最佳答案

tl;dr:Kate 回答中的解决方案适用于 OP;解释不正确。有问题的代码实际上是正确的,并且可以在 VS2015 中编译。该错误(在 VS2013 更新 3 中)是 MS 如何处理在 VS2013 RTM 中发现的错误的结果(他们实际上并没有通过更新修复它,但他们确实破坏了一些工作代码) .它已在 VS2015 中正确修复。


您的代码在 VS2015 中运行良好。 According to Microsoft , 在 VS2013 中,

Yes, we inserted these errors into the compiler in Update 3 because our implementation of non-static data member initialization was incomplete.

成员初始化列表中的列表/支撑初始化在 VS2013 中也被破坏。他们实现的问题是illustrated best带有一个 vector,它有一个 initializer_list 构造函数,它应该贪婪地匹配任何使用带有可转换参数的大括号的初始化,但它没有这样做:

struct S {
    S() : v1{1} {} // C2797, VS2013 RTM incorrectly calls 'vector(size_type)'

    std::vector<int> v1;
    std::vector<int> v2{1, 2}; // C2797, VS2013 RTM incorrectly calls 
                               // 'vector(size_type, const int &)'
};

编译器很容易退回到正常的重载解析。它不使用 std::initializer_list 构造函数,而是调用 size_t 构造函数。正如他们的评论所表明的那样,这是错误的!因此,由于其实现存在缺陷,Microsoft 决定禁用在此上下文中使用支撑初始化的能力。

对于 std::string s,应该没有问题,因为 s{"duh"} 的正确做法是 调用std::string(const char*),但是由于这个缺陷,MS无论如何都会报错。解决方法是明确使用括号代替大括号(或升级到 VS2015),如 Kate 的回答中所述。但错误的正确原因如上所述。

这适用于非静态数据成员初始化 (NSDMI) 以及初始化列表。这在 Visual C++ Team Blog 中有更多解释。 .至于为什么 VS2013 从未得到修复:

We originally planned to fix this bug in an update to Visual Studio 2013, but from an engineering perspective, the right thing to do is to avoid another kludge and thoroughly address the handling of initialization. But overhauling compiler architecture is a massive task due to the amount of fundamental code that needs to be modified. We could not risk creating incompatibilities or large bug tails in an update, so a correct implementation of NSDMI could only be shipped in a major release.

显然,此修复程序已进入 Visual Studio 2015,但永远不会出现在 2013 的更新中。

关于c++ - 错误 C2797 : list initialization inside member initializer list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741521/

相关文章:

c# - 我应该共享使用公共(public)字段的对象的 UI 吗?

c++ - 如何在 C++ 中的登录/密码窗口上使用回车键?

visual-studio - 安装后如何将应用程序文件夹添加到 %PATH%(VS 安装项目)

node.js - 控制台窗口不再出现在我的 Visual Studio Node 控制台应用程序中

c# - Visual Studio 中的 "Treat all warnings as errors except..."

javascript - 不能从上下文对象继承?

c# - 使用 AddRange 添加子类的集合

C++ SDL 2.0 - 使用循环导入多个纹理

c++ - 通过组合访问父类

c++ - 在 C++11 中,您能否将引用的基类传递给线程的构造函数并获得多态行为?