473,698 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template functio need help

Hi all,
Here is a std::vector containing some std::complex<do uble>. 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(<unkno wn 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 2205
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(<unkno wn 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********@ter ra.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(Fun Obj 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(Fun Obj 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********@ter ra.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(Fun Obj 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********@ter ra.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(Fun Obj 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
3347
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 like that possible? Some workaround? Thank you, Patrick
4
2023
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
10278
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 possible, so I need "something like" a virtual function template. I read in some threads that there is a workaround using the visitor pattern. I read therefore in the book "Modern c++ Design" about this pattern, but couldnt find any help to my...
9
2726
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: ? template <class T1, class T2> int getValue( T1 col, T2 row ) ; template <class T1, class T2> double getValue( T1 col, T2 row ) ;
1
1984
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<> void from_string(const char Str, unsigned lon &); //
3
13744
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
4
1593
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> <SPECIAL1>abc</SPECIAL1> <SPECIAL2>abc</SPECIAL2> ... <SPECIAL12>abc</SPECIAL9>
6
1827
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 quite answer all my questions. How would one create a 3 level TOC when each item level / node was differently named (They used Template match and for-each, but the template match worked as on a 3 level structure they usedf the same named xml...
19
7924
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.