Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i am not able to create the Unique pointer

Tags:

c++

c++11

c++14

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 &'
like image 484
shomit Avatar asked Dec 05 '25 04:12

shomit


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>(); 
like image 108
Arthur Tacca Avatar answered Dec 06 '25 20:12

Arthur Tacca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!