Compiler Error C2664 when passing pointer to function

Started by
14 comments, last by Tutukun 14 years, 12 months ago
i have this function

static bool TowerInfoInterface::_CompareIteratorValue(const char* c1 ,  const char* c2)
{
	return (strcmp(*c1, *c2) == 0)? true:false;
}
im trying to past it into this func

iterator find(const T &inT, bool (*obj)(const T&, const T&)) const
but it keep giving me this err

cannot convert parameter 2 from 'bool (__cdecl *)(const char *,const char *)' to 'bool (__cdecl *)(const T &,const T &)'
      with
       [
          T=char *
       ]
      None of the functions with this name in scope match the target type
i even tried to change _CompareIteratorValue to

_CompareIteratorValue (const char*& c1 ,  const char*& c2)
same err msg:

cannot convert parameter 2 from 'bool (__cdecl *)(const char *&,const char *&)' to 'bool (__cdecl *)(const T &,const T &)'
1>        with
1>        [
1>            T=char *
1>        ]
1>        None of the functions with this name in scope match the target type
any one have any suggestion? cheers :)
Advertisement
Well, if your template type T is char* then const T& evaluates to char*& const, i.e. the char pointer itself is constant, not the pointee (i.e. what it points to). Your T should be const char* IMO.
Quote:Original post by Red Ant
Well, if your template type T is char* then const T& evaluates to char*& const, i.e. the char pointer itself is constant, not the pointee (i.e. what it points to). Your T should be const char* IMO.



hmm thx, i've tried that same err msg :(

cannot convert parameter 2 from 'bool (__cdecl *)(const char *,const char *)' to 'bool (__cdecl *)(const T &,const T &)'1>        with1>        [1>            T=const char *1>        ]1>        None of the functions with this name in scope match the target type


Oops, sorry. I suppose you can't change your find() function to have this signature?

iterator find(const T &inT, bool (*obj)( T, T )) const

Try:
static bool TowerInfoInterface::_CompareIteratorValue(const char const* c1 ,  const char* const c2){  return (strcmp(c1, c2) == 0)? true:false;}


(Note that strcmp takes two char*s, not two chars so you'll also need to correct your function's body).
Quote:Original post by Red Ant
Oops, sorry. I suppose you can't change your find() function to have this signature?

*** Source Snippet Removed ***



well if i changed the lib then it would work, i just think there should be a way since the function was declared like that for a reason.


@Nitage: i tried n still doesnt work

const char *,const char *const )' to 'bool (__cdecl *)(const T &,const T &)

im using standard C strcmp i just forgot to change it back after trying different type of parameters
Quote:Original post by Nitage
Try:
*** Source Snippet Removed ***

(Note that strcmp takes two char*s, not two chars so you'll also need to correct your function's body).



Hmm, I didn't even see that. The strcmp() from the C library takes two char*s, too. But maybe he's using not using the standard C strcmp() there.

Quote:Original post by Tutukun
well if i changed the lib then it would work, i just think there should be a way since the function was declared like that for a reason.



Yeah, go with Nitage's suggestion. That should work.

@all

By the way, why does C++ even care if the pointer arguments have the same const-qualifiers in both function signatures? I mean they're just local variables ... what's it to C++ if the function changes the value of a local variable. I can understand that equal constness matters with regard to the pointee, but why the pointers themselves? Hmm.




you mean
bool TowerInfoInterface::_CompareIteratorValue(const char const* c1 ,  const char const* c2)



or
bool TowerInfoInterface::_CompareIteratorValue(const char* const c1 ,  const char* const c2)
??
The latter.

This topic is closed to new replies.

Advertisement