Why i am not able to create the Unique pointer

Viewed 132

I get c2664 error when i try to create a unique pointer

i did write the copy constructor as well as assignment operator but still i keep getting the 2664 error

class UndoCopyPaste : public UndoRedo
    {
    private:
        Container containerValue;
        bool valid;
    public:
        UndoCopyPaste() = default;
        UndoCopyPaste(Container* cont, std::string type);
        UndoCopyPaste(Container trans, Container* cont, std::string type);
        
    };
    
    class UndoRedo
    {
    private:
        std::string type;
    protected:
        Container* container;
    public:
        UndoRedo() = default;
        UndoRedo(Container* cont, std::string undoType);
    };
    
    std::unique_ptr<UndoCopyPaste> undoCopyPastePointer = std::make_unique<UndoCopyPaste>(new UndoCopyPaste()); // error C2664: 'UndoCopyPaste::UndoCopyPaste(UndoCopyPaste &&)': cannot convert argument 1 from '_Ty' to 'const UndoCopyPaste &'
1 Answers

This code with make_unique:

x = std::make_unique<Foo>(a, b);

is essentially equivalent to this:

x = std::unique_ptr<Foo>(new Foo(a, b));

Note that the a, b pass to make_unique gets passed directly to the constructor of Foo. So your code

std::make_unique<UndoCopyPaste>(new UndoCopyPaste()); 

is equivalent to this:

std::unique_ptr<UndoCopyPaste>(new UndoCopyPaste(new UndoCopyPaste())); 

You're actually passing new UndoCopyPaste() to the constructor of UndoCopyPaste! So just take out that parameter altogether:

std::make_unique<UndoCopyPaste>(); 
Related