Jump to content
  • Advertisement
Sign in to follow this  
themadme

c++ template function pointer problem

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi people,

I have a slight problem an having trouble to understand why it's causing a compile error.




error C2664: 'Lex::LinkedList<T>::setSortQuery' : cannot convert parameter 1 from 'Lex::LinkedList<T>::SortReturnCode (__cdecl *)(const Data,const Data)' to 'Lex::LinkedList<T>::SortReturnCode (__cdecl *)(const T,const T)'
with
[
T=Data *
]
None of the functions with this name in scope match the target type[/quote]

Here is a code snippet



template<class T>
class LinkedList
{
public:

enum SortReturnCode{LOWEST=1, HIGHEST=2, IDENTICAL=4};

typedef SortReturnCode (*SortQueryFncPtr)(const T Data1, const T Data2);

void setSortQuery(SortQueryFncPtr pSortQueryFncPtr)
{
m_pSortQueryFncPtr = pSortQueryFncPtr;
}

private:

SortQueryFncPtr m_pSortQueryFncPtr;
};

struct Data
{
char cTest[255];
};

static LinkedList<Data*>::SortReturnCode SortTest(const Data *Test1, const Data *Test2)
{
return LinkedList<Data*>::IDENTICAL;
}

int main()
{
LinkedList<Data*> listTest;

listTest.setSortQuery(SortTest);

return 0;
}


If I create a list container that contains a non-pointer value, (T=Data), then there is no error. Could anyone be kind to explain why I retrieve this error.

thanks

Share this post


Link to post
Share on other sites
Advertisement
Because "const T" for T = Data * is "Data * const" not "const Data *". The const binds to the pointer type not the pointed to type. You'd need the function signature to be:

static LinkedList<Data*>::SortReturnCode SortTest(Data * const Test1, Data * const Test2)

Share this post


Link to post
Share on other sites
Hi,

Thanks for the reply, SiCrane, *high five*

That makes sense I guess, I just presumed that it would const binds to the pointed type as it works for referencing type.

So how would one go about making sure it would const binds to the pointed type? I'm guessing that I would change :


[color=#660066]SortQueryFncPtr[color=#666600])([color=#000088]const[color=#000000] T [color=#660066]Data1[color=#666600],[color=#000000] [color=#000088]const[color=#000000] T [color=#660066]Data2[color=#666600])

to


[color=#660066]SortQueryFncPtr[color=#666600])([color=#000088]const[color=#000000] T *[color=#660066]Data1[color=#666600],[color=#000000] [color=#000088]const[color=#000000] T *[color=#660066]Data2[color=#666600])
?

Thanks

Share this post


Link to post
Share on other sites
Hidden
Hi,

Duh, that's bad of me, I should really read more carefully rather than get frustrated lol.

Thanks for the reply, SiCrane, *high five*

Share this post


Link to post
If T is a [type]Data *[/type], then [type]const T *[/type] would be [type]Data * const *[/type], which still isn't quite what you want. One way to address this is to use a type traits class. Something like:

template <typename T>
struct ArgType {
typedef const T value;
};

template <typename T>
struct ArgType<T *> {
typedef const T * value;
};

// in the class
typedef SortReturnCode (*SortQueryFncPtr)(typename ArgType<T>::value Data1, typename ArgType<T>::value Data2);

Share this post


Link to post
Share on other sites
Hi,

Ah that would do the trick, thanks very very much :). Never came across this problem before nor have I read about it, learn something new everyday.

Thanks very much

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!