No legal conversion to 'this' pointer

Please take a look at this code and run it:
I get a very strange error:
Error 1 error C2663: "Allocator :: allocate_help": 2 overloads do not have a legal conversion for the 'this' pointer

template<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. Thanks.

+7
c ++ templates
source share
2 answers

I noticed that Allocate declared const , but allocate_help not - could this be related to the problem?

+12
source share

I had the same error, which was also caused by const , but in a slightly different way.

I have two virtual functions (overloads), one of them was const , and the other was not. This caused a problem. It turns out that if you want to overload the function, they must match if they are const or not.

 virtual void value() const = 0; virtual void value(MyStruct & struct) = 0; 

The code above will cause this error. The fix is ​​to change 2nd ad to:

 virtual void value(MyStruct & struct) const = 0; 
0
source share

All Articles