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



 DEVELOP > c-Plus-Plus > Why can't I use vector::size_type where T1 is a template typename?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Eric Lilja"
Date: 23 Apr 2005 06:33:25 AM
Object: Why can't I use vector::size_type where T1 is a template typename?
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[i]);
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<T1,std::allocator<_CharT>

::size_type' is parsed as a non-type, but instantiation yields a type

main.cpp:24: note: say 'typename std::vector<T1,std::allocator<_CharT>

::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
.

User: "Sharad Kala sharad_mailinglist_removespam@ yahoo.com"

Title: Re: Why can't I use vector::size_type where T1 is a template typename? 23 Apr 2005 06:52:09 AM
"Eric Lilja" <mindcooler_thisshouldberemoved@gmail.com> wrote in message

#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<T1>::size_type i = 0; i < in.size(); ++i)

for(size_t i = 0; i < in.size(); ++i)
{
istringstream iss(in[i]);

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
.
User: "Eric Lilja"

Title: Re: Why can't I use vector::size_type where T1 is a template typename? 23 Apr 2005 08:25:47 AM
"Sharad Kala" wrote:


"Eric Lilja" <mindcooler_thisshouldberemoved@gmail.com> wrote in message

#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<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[i]);

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.

}


Sharad


Thanks Sharad (and Sumit of course)!
/ Eric
.


User: "Sumit Rajan"

Title: Re: Why can't I use vector::size_type where T1 is a template typename? 23 Apr 2005 06:48:24 AM
"Eric Lilja" <mindcooler_thisshouldberemoved@gmail.com> wrote in message
news:d4dbpj$msp$1@news.island.liu.se...

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<T1>::size_type i = 0; i < in.size(); ++i)

for(size_t i = 0; i < in.size(); ++i)
{
istringstream iss(in[i]);

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.
--
Sumit Rajan <sumit.rajan@gmail.com>
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER