No legal conversion to 'this' pointer
Please take a look at this code and run it:
I'm getting very strange error:开发者_如何学编程 Error 1 error C2663: 'Allocator::allocate_help' : 2 overloads have no legal conversion for 'this' pointertemplate<class FailureSignal>
class Allocator
{
private:
template<class Exception,class Argument>
void allocate_help(const Argument& arg,Int2Type<true>)
{
}
template<class Exception,class Argument>
std::nullptr_t allocate_help(const Argument& arg,Int2Type<false>)
{
return nullptr;
}
public:
template<class T>
void Allocate(signed long int nObjects,T** ptr = 0)const
{
allocate_help<std::bad_alloc>(1,Int2Type<true>());
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Allocator<int> all;
all.Allocate<int>(1);
return 0;
}
I absolutely do not understand this err msg. Hope someone can help me with this. Thank you.
I noticed Allocate
is declared const
but allocate_help
is not - could that be related to the issue?
I had the same error which was also caused by const
but in a bit different way.
I have two virtual functions (overloads), one was const
and the other was not. This was causing the problem. Turns out if you want to overload a function, they both need to match if they are const
or not.
virtual void value() const = 0;
virtual void value(MyStruct & struct) = 0;
The above code will cause this error. The fix is to change declaration of 2nd to:
virtual void value(MyStruct & struct) const = 0;
I had this problem, when i tried to change member data in a const declared method.
As we all know, const methods are supposed not to change member data. The compiler gcc/clang doesn't return good error message.
It successfully compiled after removing const specifier from
void I_am_a_const_method_but_trying_to_modify_member() const{
amap.emplace(std::make_tuple(1, 1, "a"), 1);
}
compiler explorer link
精彩评论