473,669 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I use vector<T1>::siz e_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<typena me 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(t emp);
}
}

int
main(int argc, char* argv[])
{
vector<string> numbers_as_stri ngs;
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_argumen ts(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_argumen ts(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
Jul 23 '05 #1
3 2392

"Eric Lilja" <mi************ *************** *@gmail.com> wrote in message
news:d4******** **@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<typena me 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>::siz e_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(t emp);
}
}

int
main(int argc, char* argv[])
{
vector<string> numbers_as_stri ngs;
vector<int> numbers;

convert(numbers _as_string, numbers);

^^^^^^^^^^^

Check the name. I think you meant numbers_as_stri ngs.

Regards,
Sumit.
--
Sumit Rajan <su*********@gm ail.com>
Jul 23 '05 #2

"Eric Lilja" <mi************ *************** *@gmail.com> wrote in message
#include <sstream>
#include <string>
#include <vector>

using namespace std;

template<typena me 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>::siz e_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(t emp);
}
}

int
main(int argc, char* argv[])
{
vector<string> numbers_as_stri ngs;
vector<int> numbers;

convert(numbers _as_string, numbers);
convert(numbers _as_strings, numbers);
}


Sharad
Jul 23 '05 #3

"Sharad Kala" wrote:

"Eric Lilja" <mi************ *************** *@gmail.com> wrote in message
#include <sstream>
#include <string>
#include <vector>

using namespace std;

template<typena me 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>::siz e_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(t emp);
}
}

int
main(int argc, char* argv[])
{
vector<string> numbers_as_stri ngs;
vector<int> numbers;

convert(numbers _as_string, numbers);


convert(numbers _as_strings, numbers);


Fixed thusly.
}


Sharad


Thanks Sharad (and Sumit of course)!

/ Eric
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4302
by: CFG | last post by:
Is there a ready-to-use C++ smart pointer class working on top of COM reference counting mechanism. Did anyone manage to tailor one of the available smart pointer libs to handle COM objects? Probably I can use boost::shared_ptr<T> with custom deleter - which calls obj->Release() instead of delete obj; But this workaround is not safe and wasteful (we have two separate reference counters around: first counter is an internal COM's one,...
1
1466
by: Agoston Bejo | last post by:
Hello, I've got a template related problem. How do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition: "There exists some T1 such that B is convertible to A<T1>". An example program: (Platform: VC++7.1) ---------------------------------------------------
22
130176
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the document and closes it with the following code: <body onload="window.print(); window.close();"> This works correctly (or at least the way I expect it to work under MS Internet Explorer, but it cuases Netscape to "crash"
8
4862
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
6
2890
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class My<int; What if I have a template class template <typename T, typename Uclass My ;
3
1853
by: subramanian100in | last post by:
Given two types T1 and T2, we can create pair<T1, T2p(value1, value2); where value1 and value2 are of types T1 and T2 respectively. Suppose we want to assign a new pair<value to p. We can do it as p = pair<T1, T2>(value3, value4); where value3 and value4 are of types T1 and T2 respectively. The last statement can also be written as
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.