Error passing std :: unique_ptr to class

I need to create an instance of a class that inherits from an abstract class. My code is very simple. He must create an instance of the object class based on the abstract class. An abstract class is also a template class. Then I need to put this object in a storage class that contains a pointer to the object. When passing a pointer, I get errors:

templates.cpp: In member function ‘void storage::setPTR(std::unique_ptr<child>&)’:
templates.cpp:39:28: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = child; _Dp = std::default_delete<child>]’
             this->childPTR = pointer;
                            ^
In file included from /usr/include/c++/5/memory:81:0,
                 from templates.cpp:3:
/usr/include/c++/5/bits/unique_ptr.h:357:19: note: declared here
       unique_ptr& operator=(const unique_ptr&) = delete;
                   ^
templates.cpp: In function ‘int main()’:
templates.cpp:45:30: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr
     std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");
                              ^
templates.cpp:45:30: note:   expected a type, got ‘INTERFACE’
templates.cpp:45:30: error: template argument 2 is invalid
templates.cpp:45:65: error: cannot convert ‘std::_MakeUniq<child>::__single_object {aka std::unique_ptr<child>}’ to ‘int’ in initialization
     std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");
                                                                 ^
templates.cpp:48:24: error: ‘newChild’ was not declared in this scope
     testStorage.setPTR(newChild);
                        ^

My code is:

#include <iostream>
#include <string>
#include <memory>

// using namespace std;

template<typename type1, typename type2, typename type3> class INTERFACE {
    protected:
        type1 x;
        type2 y;
        type3 name;

    public:
        virtual type1 setX(type1 x) = 0;
        virtual type2 setY(type2 y) = 0;
};

class child : public INTERFACE<int, float, std::string> {
    public:
        child(std::string z) {
            this->name = z;
        }

        virtual int setX(int x) override {
            this->x = x;
        } 

        virtual float setY(float y) override {
            this->y = y;
        }
};

class storage {
    private:
        std::unique_ptr<child> childPTR;

    public:
        void setPTR(std::unique_ptr<child> & pointer) {
            this->childPTR = pointer;
        }
};

int main(){
    // std::unique_ptr<INTERFACE> newChild(new child("xxx"));
    std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");

    storage testStorage;
    testStorage.setPTR(newChild);

    return 0;
}

What am I doing wrong?

+4
source share
1 answer
  • std::unique_ptrnot assigned for copying. However, it can be assigned to move. Therefore, use the pass-by-value parameter for the and parameter std::movein setPTR.

  • You need to specify a specific type, not a template:

    std::unique_ptr<INTERFACE<int, float, std::string>> p =
        std::make_unique<child>("xxx");
    
  • newChild, std::move .

+4

Source: https://habr.com/ru/post/1692569/


All Articles