问题
I am getting this compile error:
error: invalid conversion from 'const MyClass*' to 'MyClass*'
Here is the code:
std::tr1::shared_ptr<MyClass> myClassA;
const MyClass* myClassB;
myClassA = std::tr1::shared_ptr<MyClass>(myClassB); // error here
I think I understand the error, just don't know how to fix. I need myClassB to be a const so how to convert/copy classB to a shared_ptr?
回答1:
You'll need a shared pointer to a const object:
std::tr1::shared_ptr<const MyClass> myClassA;
^^^^^
回答2:
You can't go from a const MyClass to MyClass.
myClassA = std::tr1::shared_ptr< **const** MyClass>(myClassB);
来源:https://stackoverflow.com/questions/11484737/how-to-fix-error-invalid-conversion-from-const-myclass-to-myclass