Why can't I use vector<T1>::size_type where T1 is a template typename?

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
main.cpp:24: error: dependent-name 'std::vector said:
::size_type' is parsed as a non-type, but instantiation yields a type
main.cpp:24: note: say 'typename std::vector said:
::size_type' if a type is meant
make: *** [main.o] Error 1

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
 
S

Sumit Rajan

Eric Lilja said:
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(typename vector said:
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);

^^^^^^^^^^^

Check the name. I think you meant numbers_as_strings.

Regards,
Sumit.
 
S

Sharad Kala

Eric Lilja said:
#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(typename vector said:
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);


convert(numbers_as_strings, numbers);

Sharad
 
E

Eric Lilja

Sharad Kala said:
*/

for(typename vector<T1>::size_type i = 0; i < in.size(); ++i)

Ah, yes, of course! I *always* seem to forget when I need to use "typename".
Always.
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);


convert(numbers_as_strings, numbers);


Fixed thusly.

Thanks Sharad (and Sumit of course)!

/ Eric
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top