Archived

This topic is now archived and is closed to further replies.

Template functions and function ptrs

This topic is 5709 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, When I try to compile my program (see source below - don''t worry, its very small), I get the following error. --------------------Configuration: TestApp - Win32 Debug-------------------- Compiling... main.cpp C:\Documents and Settings\David\My Documents\Programming\Accelerated C++\chapter_8\TestApp\main.cpp(18) : error C2664: ''remove_copy_if'' : cannot convert parameter 4 from ''bool (int &)'' to ''bool (__cdecl *)(int *& )'' None of the functions with this name in scope match the target type Error executing cl.exe. TestApp.exe - 1 error(s), 0 warning(s) Can anybody explain why this is the case because if I change PredFunc() to take an int parameter instead of a reference to an int, and pred() to take a Type object instead of a referenece to a type object everything works fine. I don''t understand why I get errors when I use integer references since the signatures of PredFunc() and pred() are identical. Thanks template Output remove_copy_if(Input b, Input e, Output d, bool (pred)(Type & obj)); // function ptr bool PredFunc(int & i); int main() { using std::cout; using std::cin; using std::endl; int seq[] = { 1, 3, 6, 2, 4, 6, 8 }; int dest[2]; int * pEnd = remove_copy_if(seq, seq + 7, dest, PredFunc); for (int * pInt = dest; pInt != pEnd; pInt++) cout << *pInt << endl; return 0; } template Output remove_copy_if(Input b, Input e, Output d, bool (pred)(Type obj)) // function ptr { while (b != e) { if (pred(*b) == false) { *d = *b; d++; } b++; } return d; } bool PredFunc(int & i) { if (i % 2 == 0) return true; else return false; }

Share this post


Link to post
Share on other sites
quote:
template
Output remove_copy_if(Input b, Input e, Output d,
bool (pred)(Type & obj)); // function ptr



ehm, why do use templates at all, maybe i missunderstod how templates work, but isnt there something missing ??? shouldnt it be:
quote:
template

Output remove_copy_if(Input b, Input e, Output d,
bool (pred)(Type & obj)); // function ptr




T2k

Share this post


Link to post
Share on other sites
For some reason, the angle brackets (<) (>) don''t appear in this forum so I''ve used (( and )) instead.

template((class Input, class Output, class Type))
Output remove_copy_if(Input b, Input e, Output d,
bool (pred)(Type & obj)); // function ptr

Also, I know that remove_copy_if() is part of the STL and that its not necessary to write my own version - I''m simply working through some exercises in a textbook.

Thanks


Share this post


Link to post
Share on other sites
David,

The reason < and > are stripped out is because they are interpretted as the start and end of html tags. You can fix this in one of two ways;

1) Convert the code to html before posting.
2) Use the 'code' tags this forum supports.

Anyway your real problem with the code was incorrectly declaring the function pointer type, you missed a *.

For example

"bool foo(int & i);" has the following type "bool (*)(int &)" which would lead to the following parameter declaration:

void FunctionTakingPredicate( bool (*pred)(int &) ){
...do something with 'pred' ...
}

Anyway your fixed code becomes ( see below )

Hope this helps

Tom.



#include <iostream>

template
Output remove_copy_if(Input b, Input e, Output d, bool (*pred)(Type & obj)); // function ptr
// --------------------------------------------------> __

bool PredFunc(int & i);

int main()
{
using std::cout;
using std::cin;
using std::endl;

int seq[] = { 1, 3, 6, 2, 4, 6, 8 };

int dest[2];
int * pEnd = remove_copy_if(seq, seq + 7, dest, PredFunc);
for (int * pInt = dest; pInt != pEnd; pInt++)
cout << *pInt << endl;

return 0;
}

template
Output remove_copy_if(Input b, Input e, Output d, bool (*pred)(Type & obj)) // function ptr
// --------------------------------------------------> __ __
{
while (b != e)
{
if (pred(*b) == false)
{
*d = *b;
d++;
}
b++;
}

return d;
}

bool PredFunc(int & i)
{
if (i % 2 == 0)
return true;
else
return false;
}




[edited by - TomH on May 31, 2002 8:43:24 AM]

Share this post


Link to post
Share on other sites