Summary: Why is the ‘=’ assignment operator inaccessible with two objects of the same type std::unique_ptr<Expression, std::default_delete<Expression>>?
Mind you, in the code the types are written std::unique_ptr<Expression> but expand to the former in the IntelliSense error.
Info
IDE – Running Visual C++ 2010
OS – Windows Vista
Target – Console Application attempting to use what’s implemented of C++11
Some Background
I’ve been provided an example of an expression evaluator in C++ which apparently is up to the new standard alright, because what should be solid code is giving me errors and I’m guessing it’s because VC++ has no support for the feature. The first error I got was with the ‘make_unique’ method, VC++ said “identifier doesn’t exist”, so I implemented it myself
template<typename T>
std::unique_ptr<T> make_unique()
{
return std::unique_ptr<T>(new T());
} // Basically just wrapping 'new'? Not sure why
Found a better one online but it gave errors, this ^ gives no errors. All I did was remove the …Args template parameter. I have a set of Expression classes that looks like this:
class Expression {
virtual ~Expression() {}
};
class BinaryExpression : public Expression {
public:
std::unique_ptr<Expression> lhs;
std::unique_ptr<Expression> rhs;
virtual char GetType() const = 0; // +,-,/,*
};
// Then MulExpression, DivExpression, PlusExpression, blah blah
Now here’s where I get the error, mind you, ParseAdditiveExpression()’s return type is
// expr, lhs, and rhs are all of the same type
std::unique_ptr<Expression>:
auto rhs = ParseAdditiveExpression();
auto expr = make_unique<MulExpression>(); // Tried using: std::unique_ptr<Expression> expr;
expr->lhs = lhs; // Trouble Makers
expr->rhs = rhs; // All produce
lhs = expr; // The same errors
The error is as follows:
1 IntelliSense: "std::unique_ptr<_Ty, _Dx> &std::unique_ptr<_Ty, _Dx>::operator=(const std::unique_ptr<_Ty, _Dx> &) [with _Ty=Expression, _Dx=std::default_delete<Expression>]"
(declared at line 2352 of "C:\Program Files\Microsoft Visual Studio 10.0\VC\include\memory")
is inaccessible
c:\users\s_miller47\documents\visual studio 2010\projects\_vc++\powercalc\mathparser.h 133
Now, I think this may have something to do with the ‘const’ at the right size of the ‘=’, but I’ve tried changing the functions return type to fit, with * or & after the return type, tried de-referencing (‘*’) the value before I set it to expr->lhs, even tried (‘&’)
So if the operator is inaccessible how do I fix it? Do I have to define the operator myself? (nonsense) Well, here is the full code: PasteBin Source Code Link
std::unique_ptris non-assignable and non-copyable. If you want to make such assignment, you will need to usestd::move():