iterators as parameters in template functions

Hi -

I'm trying to create a template function. Here's the .h file:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

template <class T> int getVectort(ifstream & s,
				   long	nbrCells,
				   typename vector<T>::iterator a);


And here's the .cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

template <class T> int	getVectort(ifstream& s,
				  long nbrCells,
				  typename vector<T>::iterator iter)
{
	int		rc = 0;
	int32_t	temp;
	for (int i = 0; i < nbrCells; i++)
	{
		s >> hex >> temp;
		*iter++ = temp;
	}
	if (s.fail())
		rc = 1;
	return rc;
}


They compile OK.

But...when I try to use one, like this:

1
2
3
4
5
	vector<int32_t>::iterator iter;
.
.
.
		rc = getVectort(fileIn, DEMOD_NBR_INPUTS, iter);


I get this build error:

error: no matching function for call to 'getVectort(std::ifstream&, const long int&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)'


What am I doing wrong? Thanks.
templates should be defined in the header file.
Still not compiling; thanks for the reminder on that, though. I combined them into one .h file and removed the .cpp file from my configuration. I appear to be getting the same error:

error: no matching function for call to 'getVectort(std::ifstream&, const long int&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)'


Is there something wrong with the way I defined the template parameter?

EDIT:

Just to experiment, I removed all arguments except for the iterator from the function definition, and the call to it. Effectively the same error message.

Any ideas? Thanks.
Last edited on
You're trying to use automatic type deduction, but it won't work like that (it can't deduce the template argument of std::vector<T> !).

I'd suggest the following

1
2
3
4
5
6
template <class Iterator>
int getVectort(/* whatv... */  Iterator a)
{
typedef typename Iterator::value_type T;
// ...
}


So you also have the value type. Every iterator must have a value_type, otherwise it's not an iterator.

EDIT: added typename
Last edited on
Or you can of course use you code and just specify the template argument

1
2
vector<int32_t>::iterator iter;
rc = getVectort<int32_t>(fileIn, DEMOD_NBR_INPUTS, iter);
Last edited on
I don't think I understand your first example, Dario. If it's not too much trouble, could you elaborate?

I already tried the second example you gave, and it doesn't change the error message.

Thanks.
Solved...cockpit error.
Last edited on
Hey.

It means you'd have to call the function like this

1
2
std::vector<int32_t> vec(n,1);
rc = getVectort(/*whatv...*/ vec.begin());


And the body of your function doesn't change, except when you have to explicitly use the typename T (like during instantiation). However, I also saw that in your original code you set

 
*iter++ = temp;


Where temp is an int32_t regardless of the template parameter type. Seems wrong.
Where temp is an int32_t regardless of the template parameter type. Seems wrong.


this statement seems confusing to me please can you elobrate
And also i suppose the return type you have put is int ... which should be T .
correct me if i am wrong.

Thanks in advance .
Last edited on
Topic archived. No new replies allowed.