invalid initialization of non-const reference of type 'Tree&' from an rvalue of type 'Tree' in passing argument 2 of 'void swap(Tree&, Tree&)'
C++ does not allow passing anonymous objects by non-const
reference. The intent is to prevent callers from accidentally throwing away the results of functions that write to a reference argument.
You instead could do:
Tree::Tree(const double &a, const double &b, int depth)
{
double aTemp(a), bTemp(b);
Tree temp(&aTemp, &bTemp, depth, depth);
swap(*this, temp);
}
But it does not seem to be working either. I think the problem is that when I call the copy-assignment operator (*this = Tree(&aTemp, &bTemp, depth, depth)), the copy constructor should be called (since the argument of the copy-assignement operator is passed by value), but it seems that this is not happening. I do not understand why.
How are you determining that it's not working? The compiler might elide away the copy to avoid doing unnecessary work. (That is why your copy-assignment operator takes the argument by value.)
BTW, if your compiler supports C++11, you could use delegating constructors instead.