473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template functio need help

Hi all,
Here is a std::vector containing some std::complex<double>. I would
like to take the amplitude of the complex array.
i.e. v = [c1, c2, c3, c4 ...]
amp of v = [abs(c1)^2, abs(c2)^2, abs(c3)^2, abs(c4)^2,
abs(c5)^2]
I try a template function but didn't work

std::vector< complex<double> > v(5);
std::vector< double > av(5);

// initialize v here
....

transform( v.begin(), v.end(), av.begin(), ptr_fun(abs) );

Here abs is a template function defined in std. The error message is

" no matching function for call to `ptr_fun(<unknown type>)' "

It seems that the system don't know the function abs, so I try this

template <typename T>
T myabs(complex<T> c)
{
return abs(c);
}

transform( v.begin(), v.end(), av.begin(), ptr_fun(myabs) );

Now, it works fine. I wonder why it fails in the first transform
???????

Anyway, what I get now is only the abs of the complex element (|u|)
but not the amplitude (|u|^2). The problem can obviously be solved by
taking the transform again like

transform( av.begin(), av.end(), av.begin(), bind2nd(ptr_fun(pow),
2.0) );

However, it really take times when the above code is running. If it's
possible to obtain the last result with only one trasnform? And how?

Thanks in advance.
Jul 19 '05 #1
7 2189
Rex_chaos escribió:
// initialize v here
...

transform( v.begin(), v.end(), av.begin(), ptr_fun(abs) );

Here abs is a template function defined in std. The error message is

" no matching function for call to `ptr_fun(<unknown type>)' "
Try this:

std::transform( v.begin(), v.end(), av.begin(), std::ptr_fun (&
std::abs<double> ) );
Anyway, what I get now is only the abs of the complex element (|u|)
but not the amplitude (|u|^2). The problem can obviously be solved by
taking the transform again like

transform( av.begin(), av.end(), av.begin(), bind2nd(ptr_fun(pow),
2.0) );

However, it really take times when the above code is running. If it's
possible to obtain the last result with only one trasnform? And how?


Do the same you make with myabs, write a myamplitude functor.

Regards.
Jul 19 '05 #2
> std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&
std::abs<double> ) );

Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.
Jul 19 '05 #3
Rex_chaos escribió:
std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&
std::abs<double> ) );

Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.


I put the std:: for clarity (and it seems I obscured things instead),
but the key is to use abs<double> instead of abs alone.

Regards.
Jul 19 '05 #4
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Rex chaos escribi :
std::transform( v.begin(), v.end(), av.begin(), std::ptr fun (&
std::abs<double> ) );

Are u sure it's ok? Actually, I have already add 'using namespace
std;' at the beginning of the source, still no help.


I put the std:: for clarity (and it seems I obscured things instead),
but the key is to use abs<double> instead of abs alone.


It's confusing. Take a look at this class

template <typename A>
class Tester
{
...
A myfun(A a) {...}
template <typename FunObj>
void mytransform(FunObj fun)
{
transform( v.begin(), v.end(), v.begin(), fun);
}

void testing(void)
{
mytransform( myfun );
}
}

when calling testing(), error occurs. it's all right unless I replace
myfun with an external function ??????
Jul 19 '05 #5
Rex_chaos escribió:
template <typename A>
class Tester
{
...
A myfun(A a) {...}


template <typename FunObj>
void mytransform(FunObj fun)
{
transform( v.begin(), v.end(), v.begin(), fun);
}

void testing(void)
{
mytransform( myfun );
}
}

when calling testing(), error occurs. it's all right unless I replace
myfun with an external function ??????


You can't use a member function directly with a standard algorithm, they
expect something that can be called directly.

Regards.
Jul 19 '05 #6
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Rex chaos escribi :
template <typename A>
class Tester
{
...
A myfun(A a) {...}


template <typename FunObj>
void mytransform(FunObj fun)
{
transform( v.begin(), v.end(), v.begin(), fun);
}

void testing(void)
{
mytransform( myfun );
}
}

when calling testing(), error occurs. it's all right unless I replace
myfun with an external function ??????


You can't use a member function directly with a standard algorithm, they
expect something that can be called directly.


It's annoything. I am trying to have an inner-class casting a function
object. However, no help.
Jul 19 '05 #7
On 7 Oct 2003 04:25:17 -0700, re*******@21cn.com (Rex_chaos) wrote:
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Rex chaos escribi :
> template <typename A>
> class Tester
> {
> ...
> A myfun(A a) {...}
>

>

> template <typename FunObj>
> void mytransform(FunObj fun)
> {
> transform( v.begin(), v.end(), v.begin(), fun);
> }
>

> void testing(void)
> {
> mytransform( myfun );
> }
> }
>

> when calling testing(), error occurs. it's all right unless I replace
> myfun with an external function ??????


You can't use a member function directly with a standard algorithm, they
expect something that can be called directly.


It's annoything. I am trying to have an inner-class casting a function
object. However, no help.


You can use a binder, from the <functional> header:

void testing()
{
mytransform(std::bind1st(std::mem_fun(&Tester<A>:: myfun), this));
}

Tom
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
4
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS>...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.