Skip to main content
GameDev.net gamedev.net
🔒 Locked

Compiler Error C2664 when passing pointer to function

Started by Tutukun May 26, 2009 at 1:27 PM 14 replies 5.5k views
Original Post
Tutukun
Tutukun
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 :)
Red Ant
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.
Tutukun
Tutukun
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


Red Ant
Red Ant
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

Nitage
Nitage
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).
Tutukun
Tutukun
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
Red Ant
Red Ant
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.

Red Ant
Red Ant
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.




Tutukun
Tutukun
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)
??
Red Ant
Red Ant
The latter.
Sneftel
Sneftel
Quote:
Original post by Red Ant
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.

The existence of const function arguments is a great demonstration of the silliness inherent in the C++ const model. Nevertheless, it really does make sense to disallow coercion of this sort, because constness can certainly play into calling conventions. For instance, a particular hypothetical compiler on a hypothetical architecture might decide to pass const arguments through registers that were slower to write than others. (I know, that's a bit contrived.) A function pointer would then have to keep around not only the address to jump to, but information on which arguments need to be passed in different registers. It's the same reason why function pointers don't support argument-type contravariance or return-type covariance.
Tutukun
Tutukun
Quote:
Original post by Nitage
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:

cannot convert parameter 2 from 'bool (__cdecl *)(const char *const ,const char *const )' 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


no luck :(
i might just add an extra function find(T, (bool*)(T,T))
but i really want to know the solution for this
Red Ant
Red Ant
Well, I'll take one final shot at this.

static bool TowerInfoInterface::_CompareIteratorValue(const char const*& c1 , const char*& const c2){    return (strcmp(c1, c2) == 0)? true:false;}
visitor
visitor
Why not make the find function take a "predicate" as the second template argument?

template <class T, class Predicate>iterator find(const T&, Predicate compare)


This, I believe, is how STL does it. You don't have to worry about unsuitable predicates, since the usage of the predicate would fail to compile, and it is more generic as you can also use function objects.

return (strcmp(c1, c2) == 0)? true:false;


Hm, is the ternary operator really necessary here? :)
gpu_fem
gpu_fem
You have to do a typedef on char*. This way the compiler knows that you want to have passed a const reference to a char* (whose data can be modified), as opposed to something else.


template
int find(const T&, bool (*)(const T&, const T&)) {

return 0;
}

typedef char* S;

bool comp(const S &c1, const S &c2) {

return strcmp(c1, c2)==0?true:false;
}

int main() {


char* s;
find (s, comp);

return 0;
}
Tutukun
Tutukun
Quote:
Original post by gpu_fem
You have to do a typedef on char*. This way the compiler knows that you want to have passed a const reference to a char* (whose data can be modified), as opposed to something else.




lol i just wake up, try ur suggestion n it works

so simple, why didnt I think of that
thx u :D

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.