E
Eric Lilja
Hello, I was working on a program where I needed to take a vector storing
objects of type std::string and convert each object to an int and store the
ints in another vector. I decided that I should create a templated function
for this because I've done similar things before and expect to do similar
things again (i.e., converting a collection of one type to a collection of
another type).
This is my test program, it compiles but if I try to write the for-loop as
the comment in the code says, I get errors (pasted below).
The code (this compiles):
#include <sstream>
#include <string>
#include <vector>
using namespace std;
template<typename T1, typename T2>
void convert(const vector<T1>& in, vector<T2>& out)
{
/*for(vector<T1>::size_type i = 0; i < in.size(); ++i) doesn't compile */
for(size_t i = 0; i < in.size(); ++i)
{
istringstream iss(in);
T2 temp;
iss >> temp;
out.push_back(temp);
}
}
int
main(int argc, char* argv[])
{
vector<string> numbers_as_strings;
vector<int> numbers;
convert(numbers_as_string, numbers);
}
The compilation errors I get for the size_type-version of the for loop
(using gcc 4.0.0):
g++ -Wall -W -ansi -pedantic -g -c -o main.o main.cpp
main.cpp: In function 'void convert_arguments(const std::vector<T1,
std::allocator<_CharT> >&, std::vector<T2, std::allocator<_T2> >&)':
main.cpp:24: error: expected `;' before 'i'
main.cpp:24: error: 'i' was not declared in this scope
main.cpp: In function 'void convert_arguments(const std::vector<T1,
std::allocator<_CharT> >&, std::vector<T2, std::allocator<_T2> >&) [with T1
= std::string, T2 = int]':
main.cpp:63: instantiated from here
I know the line numbers seem suspicious, but the real file contains code
that I have commented out prior to posting.
So why doesn't that work?
Thanks for any replies
/ E
objects of type std::string and convert each object to an int and store the
ints in another vector. I decided that I should create a templated function
for this because I've done similar things before and expect to do similar
things again (i.e., converting a collection of one type to a collection of
another type).
This is my test program, it compiles but if I try to write the for-loop as
the comment in the code says, I get errors (pasted below).
The code (this compiles):
#include <sstream>
#include <string>
#include <vector>
using namespace std;
template<typename T1, typename T2>
void convert(const vector<T1>& in, vector<T2>& out)
{
/*for(vector<T1>::size_type i = 0; i < in.size(); ++i) doesn't compile */
for(size_t i = 0; i < in.size(); ++i)
{
istringstream iss(in);
T2 temp;
iss >> temp;
out.push_back(temp);
}
}
int
main(int argc, char* argv[])
{
vector<string> numbers_as_strings;
vector<int> numbers;
convert(numbers_as_string, numbers);
}
The compilation errors I get for the size_type-version of the for loop
(using gcc 4.0.0):
g++ -Wall -W -ansi -pedantic -g -c -o main.o main.cpp
main.cpp: In function 'void convert_arguments(const std::vector<T1,
std::allocator<_CharT> >&, std::vector<T2, std::allocator<_T2> >&)':
main.cpp:24: error: expected `;' before 'i'
main.cpp:24: error: 'i' was not declared in this scope
main.cpp: In function 'void convert_arguments(const std::vector<T1,
std::allocator<_CharT> >&, std::vector<T2, std::allocator<_T2> >&) [with T1
= std::string, T2 = int]':
main.cpp:63: instantiated from here
main.cpp:24: error: dependent-name 'std::vector said:::size_type' is parsed as a non-type, but instantiation yields a type
make: *** [main.o] Error 1main.cpp:24: note: say 'typename std::vector said:::size_type' if a type is meant
I know the line numbers seem suspicious, but the real file contains code
that I have commented out prior to posting.
So why doesn't that work?
Thanks for any replies
/ E