在Visual Studio 2012中使用“= delete”时编译器错误

时间:2017-02-02 11:57:45

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

我有这个课程,在编译时,会给出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上编译没有任何问题。注意:如果我对代码编译的行进行注释。

1 个答案:

答案 0 :(得分:4)

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