Passing STL container to temlate function

A

Active8

vector<double> signal;
vector<double>::iterator iter;

in_file.load_vector(signal);
in_file.load_vector(signal.begin());

those calls give the compiler error:
"could not deduce template argument for 'T'"

the infile object is a non-template class with a template function
in it

class Files
{
...
template <class T>
load_vector(vector<T>& sig);
...
};

template <class T>
int Files::load_vector(vector<T>& sig)
{
return 0;
}

What can I do about this?

TIA
 
A

Alf P. Steinbach

* Active8:
vector<double> signal;
vector<double>::iterator iter;

in_file.load_vector(signal);
in_file.load_vector(signal.begin());

those calls give the compiler error:
"could not deduce template argument for 'T'"

the infile object is a non-template class with a template function
in it

class Files
{
...
template <class T>
load_vector(vector<T>& sig);
...
};

template <class T>
int Files::load_vector(vector<T>& sig)
{
return 0;
}

What can I do about this?

To do: be more precise (most of the information you've provided is
irrelevant, and the word "those" is incorrect; with the irrelevant
information removed and that word corrected you have your solution).
 
A

Active8

* Active8:

To do: be more precise (most of the information you've provided is
irrelevant,

Other than the compiler (VC++) what info *would* you consider
"relevant"? The code I posted is all the code (except compiler
supplied headers) pertaining to the template function that won't
compile. I included the call that I used when I tried passing an
iterator.
and the word "those" is incorrect;

no it isn't.
with the irrelevant
information removed and that word corrected you have your solution).

calls give the compiler error:
"could not deduce template argument for 'T'"

Doesn't help. AFAICT, the syntax is right and it should work like
any other code written the same way like this code from an online
ref.

template <class T>
T GetMax (T a, T b) {
return (a>b?a:b);
}

int main () {
int i=5, j=6, k;
long l=10, m=5, n;
k=GetMax(i,j);
n=GetMax(l,m);
cout << k << endl;
cout << n << endl;
return 0;
}

That ref also shows this calling convention:

in_file.load_vector<double>(signal);

which generates this error:

"type 'double' unexpected"
 
A

Alf P. Steinbach

* Active8:
Other than the compiler (VC++) what info *would* you consider
"relevant"?

The signature of the function called, the call, the error message,
and possibly also the compiler (in this case it isn't compiler-specific
but you couldn't know that), for best effort the sig+call packaged as
a small program that one can copy, paste and compile.

The code I posted is all the code (except compiler
supplied headers) pertaining to the template function that won't
compile. I included the call that I used when I tried passing an
iterator.


no it isn't.

Well, it is. ;-)

Consider the signature of your function again.

Hint 1: in there you'll see the word "vector". Hint 2: one of your
calls doesn't pass a "vector". Hint 3: the other one does.
 
A

Active8

Consider the signature of your function again.

Hint 1: in there you'll see the word "vector". Hint 2: one of your
calls doesn't pass a "vector". Hint 3: the other one does.

I finally found that, duh!

I'll post a small complete module next time, but notice that I was
correct in asuming that what I posted was enough :)
 

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,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top