Skip to main content
gamedev.net
🔒 Locked

Template functions and function ptrs

Started by Rappo_David 3 replies 600+ views
Original Post
Rappo_David
Rappo_David
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; }
T2k
T2k
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
Rappo_David
Rappo_David
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


TomH
TomH
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]

Topic Locked

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

Sign in to reply to this topic.