0

unique_ptrプロジェクトに次のコードがありますが、行が間違っているというエラー C2059, syntax error "new" が表示されます。

#include <memory>

class Nothing {
public:
    Nothing() { };
};
class IWriter
{
    public:
        IWriter() {
        }

        ~IWriter() {
        }
    private:
        std::unique_ptr<Nothing> test(new Nothing());
};

ここで何が起きてるの?

4

1 に答える 1

4

デフォルトのメンバー初期化子を使用しようとしていますが、間違った方法です。これは、単にブレース初期化子(または equals 初期化子) でなければなりません (メンバー宣言に含まれています)。

リストの初期化を使用できます(C++11 以降):

std::unique_ptr<Nothing> test{ new Nothing() };

またはメンバー初期化リスト:

class IWriter
{
    public:
        IWriter() : test(new Nothing) {
        }
        ~IWriter() {
        }
    private:
        std::unique_ptr<Nothing> test;
};
于 2016-08-23T09:15:10.110 に答える