dimanche 27 juin 2021

Error of defining brace-enclosed initializer list when I use std::move

I wrote the c++ code, because im learning c++ and practicing struct, class, move ctr, copy constructor, inheritance, unique pointer, etc. however, It provided me the following error when I want to use std::move in the last line of code with the raw pointer, does anyone knows how can I solve the issue?

#include <iostream>
#include<memory>


struct family{
    int x{};
    int y{};
    int z{};

    void show();
    family();
    ~family();
    family(int i, int j, int k);


    family& operator = (const family& v); //l -value ref
    family& operator=(family&& v);

};


void family::show(){
    std::cout<< x << y << z <<std::endl;
}

family::family(int i, int j, int k)
:x{i},y{j},z{k}
{std::cout << "custom ctor\n";}

family::family()=default;
family::~family()= default;


struct family2{
    int xx{};
    int yy{};
    virtual void show() const noexcept { std::cout << "Bau\n"; }
    family2();
    ~family2();
    family2(  int f,  int g);
    
};


family2::family2()=default;
family2::~family2()= default;
family2::family2(  int f,  int g) : xx{f},yy{g}{}


struct pr : public family2{
    pr() = default;
    ~pr()= default;
    pr(  int xx,  int yy) : family2{xx, yy} {}
    void show() const noexcept override { std::cout << "mau\n"; }

};

int main(){

family kkk{1,2,3};
kkk.show();

family lll;
lll.x= 8;
lll.show();


auto ooo= std::make_unique< family> (7,8,9);
ooo->show();


std::unique_ptr <family> unq (new family (88,88,88));
//std::unique_ptr <family> ppp (88,88,88); error
unq->show();

std::unique_ptr <family >unq2 (std::move(unq));
unq2->show();

unq.reset(new family(9,9,90));
unq->show();


family *uu = unq.release();
//uu->x= 999999;
uu->show();

if(unq == nullptr)
    std::cout<< "oooo" <<std::endl;

 family2* rrr = new pr{11, 11};  // daynamic polymorphysem
    std::cout << "through pointer\n";
    rrr->show();

family2 x3{std::move(rrr)};  // move ctor
x3->show();

}



no matching function for call to 'family2::family2(<brace-enclosed initializer list>)'
   94 | family2 x3{std::move(rrr)};  // move ctor
      |                          ^
tam5.cpp:47:1: note: candidate: 'family2::family2(int, int)'
   47 | family2::family2(  int f,  int g) : xx{f},yy{g}{}
      | ^~~~~~~
tam5.cpp:47:1: note:   candidate expects 2 arguments, 1 provided
tam5.cpp:45:1: note: candidate: 'family2::family2()'
   45 | family2::family2()=default;
      | ^~~~~~~
tam5.cpp:45:1: note:   candidate expects 0 arguments, 1 provided
tam5.cpp:34:8: note: candidate: 'constexpr family2::family2(const family2&)'
   34 | struct family2{
      |        ^~~~~~~
tam5.cpp:34:8: note:   no known conversion for argument 1 from 'std::remove_reference<family2*&>::type' {aka 'family2*'} to 'const family2&'
tam5.cpp:95:3: error: base operand of '->' has non-pointer type 'family2'
   95 | x3->show();
      |   ^~


Aucun commentaire:

Enregistrer un commentaire