why does compiler complain "no matching function for call to 'swap'" once i define move assignment operator
Jan 30, 2015 at 11:33pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
class test{
public :
test(int );
test(const test&);
test& operator =(test rhs);
test(test&&);
test& operator =(test&& rhs);
private :
int *p = nullptr ;
};
test::test(int i): p(new int (i)){}
test::test(const test& rhs): p(new int (*rhs.p)){}
test& test::operator =(test rhs) {
using std::swap;
swap(*this , rhs);
return *this ;
}
test::test(test&& rhs): p(rhs.p){rhs.p = nullptr ;}
test& test::operator =(test&& rhs) {
p = rhs.p;
rhs.p = nullptr ;
return *this ;
}
every thing is right before i define move assignment operator. but once i do, compiler complains "no matching function for call to 'swap'" at line 18
why?
thanks for reading, i really need some help!
Last edited on Jan 31, 2015 at 1:07am UTC
Jan 31, 2015 at 5:01am UTC
your type is no longer move-assignable because overload resolution cannot decide between operator=(test) and operator=(test&&).
Either separate move from copy: operator=(const test&) and operator=(test&&), or combine them as operator=(test).
Jan 31, 2015 at 1:04pm UTC
thank you very much!
Topic archived. No new replies allowed.