PaulTheHacker
PaulTheHacker

Reputation: 189

C++ Problem with Function Pointer in Template Class

I have written a simple template class which gives me some problems when I attempt to launch one of its methods.

The (minimum complete) example below shows my problem: I have defined a template class containing a function pointer which is set in the constructor.

#include <windows.h>

template <class T1> class T1Class
{
public:
    typedef T1 ( *TCopyNodeData )(const T1& SrcData);

    T1Class ( TCopyNodeData CopyNodeData )
    {
        //.....
    }
};

unsigned int CopyIData ( const unsigned int& IData )
{
    //.....
    return 0;
}

char* CopySData ( const char*& NData )
{
    //.....
    return nullptr;
}

int APIENTRY wWinMain ( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow )
{
    T1Class<unsigned int> MyT1Class1 ( CopyIData ); // ok
    T1Class<char*> MyT1Class2 ( CopySData );    // *****  E0289, C2664

    return 0;
}

//  E0289   no instance of constructor "T1Class<T1>::T1Class [with T1=char *]" matches the argument list
//          argument types are : (char *(const char *&NData))
//  C2664   'T1Class<char *>::T1Class(T1Class<char *> &&)' : cannot convert argument 1 from 'char *(__cdecl *)(const char *&)' to 'char *(__cdecl *)(const T1 &)'

Now, the first constructor (unsigned int) compiles fine, the second (char*) does not. I do not understand the error message; if I replace T1 with char* in that message I do have two identical parameter types and nothing should be wrong.

Or -- Am I missing something?

Upvotes: 2

Views: 114

Answers (1)

bolov
bolov

Reputation: 75707

const unsigned int& IData

here IData is a const reference to unsigned_int

const char *& NData

here NData is a mutable reference to pointer to to const char.

So you can see the above are not equivalent in this regard and so the template can't match.

The fix is to move the const qualifier:

char* CopySData (char * const& NData )

Upvotes: 2

Related Questions