template function cant be found

B

bluekite2000

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=abs(Vin);


}


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> >&)'
 
C

cipherpunk

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. :)
 
B

bluekite2000

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=abs(Vin);


}


int main(void)
{
vector<CS> Vin(3,2);

vector<float> Vout=abso(Vin);

return 0;
}
 
A

Alipha

[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=abs(Vin);

}

[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=abs(Vin);


}

[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top