R
Rares Vernica
Hello,
I am trying to write a generic function that can read data from a file
and store it in a container.
I defined the following function:
template<class T>
void readBinaryFile(
const std::string &filename,
std::iterator<std:utput_iterator_tag, T> data)
{
....
}
Because the function is reading binary data, it needs to know the type of
the elements in the container, T.
When I call it with something like:
vector<float> f;
readBinaryFile<float>("test.bin", back_inserter(f));
I get:
error: no matching function for call to ‘readBinaryFile(std::string&,
std::back_insert_iterator<std::vector<float, std::allocator<float> > >)’
I am a little bit confused because I know that the back_insert_iterator
is an output iterator. In fact, back_insert_iterator is defined as:
template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
If I change the function declaration to:
template<class T, class OutputIterator>
void readBinaryFile(
const std::string &filename,
OutputIterator data)
it works fine.
If possible, I would prefer to make the first declaration work
somehow. Or at least, understand why it does not work.
Thanks,
Rares
I am trying to write a generic function that can read data from a file
and store it in a container.
I defined the following function:
template<class T>
void readBinaryFile(
const std::string &filename,
std::iterator<std:utput_iterator_tag, T> data)
{
....
}
Because the function is reading binary data, it needs to know the type of
the elements in the container, T.
When I call it with something like:
vector<float> f;
readBinaryFile<float>("test.bin", back_inserter(f));
I get:
error: no matching function for call to ‘readBinaryFile(std::string&,
std::back_insert_iterator<std::vector<float, std::allocator<float> > >)’
I am a little bit confused because I know that the back_insert_iterator
is an output iterator. In fact, back_insert_iterator is defined as:
template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
If I change the function declaration to:
template<class T, class OutputIterator>
void readBinaryFile(
const std::string &filename,
OutputIterator data)
it works fine.
If possible, I would prefer to make the first declaration work
somehow. Or at least, understand why it does not work.
Thanks,
Rares