【问题标题】:Compiler error when using "= delete" in Visual Studio 2012在 Visual Studio 2012 中使用“= delete”时出现编译器错误
【发布时间】:2017-06-19 11:47:18
【问题描述】:

我有这个类,在编译时会给出 C2059 和 C 2238 ';'两条线上的两个错误。为什么这段代码不能编译?

class bitreader
{
    std::istream& §is;
    std::uint8_t §buff;
    int §n;

    uint32_t read()
    {
        if (§n == 0) {
            §buff = §is.get();
            §n = 8;
        }

        §n--;

        return (§buff >> §n) & 1;
    }

public:

    bitreader(std::istream& os)
        : §is(os)
        , §n(0)
    {}

    // The following two lines produce errors
    bitreader(const bitreader& rhs) = delete;
    bitreader& operator=(const bitreader& rhs) = delete;

    uint32_t operator()(uint32_t n)
    {
        uint32_t val = 0;

        while (n-- > 0)
            val = (val << 1) | read();

        return val;
    }

    std::istream& operator()(uint32_t& val, uint32_t n)
    {
        val = 0;
        while (n-->0)
            val = (val << 1) | read();

        return §is;
    }
};

我补充说,相同的代码在我朋友的 Visual Studio 上编译没有任何问题。注意:如果我注释代码编译的行。

【问题讨论】:

  • 这是一个有趣的数据成员命名方案。
  • 因为我大学的机器已经安装了,我们无法安装我们想要的。因此,如果我想通过考试,我必须处理实验室中安装的版本
  • 请正确缩进您的代码,以便我们阅读。
  • 另外你应该已经构建了一个minimal reproducible example - 你只需要一个非常小的程序来显示= delete 不工作。您发布的大部分代码都是无关紧要的。在向自己证明问题是 = delete 之后,您将执行 research 以找出 VS2012 中 = delete 的问题,并且可能自己找到了答案。我最初赞成这个问题的格式正确,但由于缺乏研究工作,我改变了主意。
  • @user3416648 - 因为我大学的机器已经安装了它,我们无法安装我们想要的东西。 - 如果你的朋友有 VS 2015,那就准备好解决更多问题,并且代码无法为 2012 年编译。

标签: c++ visual-studio visual-studio-2012


【解决方案1】:

The =delete specifier is a C++11 feature that Visual Studio 2012 does not support。将您的 Visual Studio 升级到较新版本,或删除 =delete 并将这两个声明设为私有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多