473,326 Members | 2,133 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,326 software developers and data experts.

template function cant be found

Here is the code:
#include <iostream>
#include <vector>
#include <complex>
typedef std::complex<double> CD;
typedef std::complex<float> CS;
using std::vector;
template<typename T1,typename T2>
vector<T2> abso(const vector<T1>& Vin)
{
vector<T2> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);
}
int main(void)
{
vector<CS> Vin(3,2);

vector<float> Vout=abso(Vin);

return 0;
}

The error is:
error: no matching function for call to `abso(std::vector<CS,
std::allocator<CS> >&)'

Aug 30 '05 #1
3 1129
You're not giving abso enough information to decide what to do. Try

vector<float> Vout=abso<CS, float>(Vin);

.... instead. As a word of warning, you have a few other errors in your
code, too; but since this looks a lot like a homework problem, I'll
leave finding those errors up to you. :)

Aug 30 '05 #2
I dont like that syntax very much and have decided to use function
overload instead. What are the pros and cons of such approach??

vector<float> abso(const vector<CS>& Vin)
{
vector<float> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);
}
int main(void)
{
vector<CS> Vin(3,2);

vector<float> Vout=abso(Vin);

return 0;
}

Aug 31 '05 #3

bl**********@gmail.com wrote:
[snip]

template<typename T1,typename T2>
vector<T2> abso(const vector<T1>& Vin)
{
vector<T2> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);

}

[snip]


I dont like that syntax very much and have decided to use function
overload instead. What are the pros and cons of such approach??

vector<float> abso(const vector<CS>& Vin)
{
vector<float> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);
}

[snip]


Well, if you want the function to work for various vector types, you'd
have to rewrite the same code for each overload.

Note that with the original code, if you reverse the template
arguments, you only have to specify one of the arguments, the one that
can't be deduced:

template<typename T2,typename T1>
vector<T2> abso(const vector<T1>& Vin) { ... }

int main() {
...
vector<int> ret = abso<int>(otherVec);
}

Or, you could not return a vector at all, and then all the arguments
would be deduced:

template<typename T1,typename T2>
void abso(const vector<T1>& Vin, vector<T2> &Vout) { ... }

int main() {
...
abso(in, out);
}

you also might want to consider std::transform instead or std::valarray.

Aug 31 '05 #4

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

Similar topics

8
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
5
by: cpunerd | last post by:
I'm confused as to the syntax for partial template specialization. Below is a very small example. Can someone tell me what I should do to make this compile properly on an ISO-compliant compiler? ...
0
by: alexroat | last post by:
Hi, I cant understand why this code does not work. I've implemented two classes A,B that provide an array like structure (they are identical). I can convert B in A using implicit conversion, but...
8
by: hurcan solter | last post by:
given the code snippet; template<typename T> void foo(T,T){} template<typename T1,typename T2> void foo(T1*,T2*){} int main( ) {
5
by: alind | last post by:
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want...
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
2
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function......
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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.