473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterator-Template problem

Hi,

I have this code

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator i;

for (i = v.begin();i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}

int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
when i tried to compile, it gave

ch3list.cpp: In function `void print_list(cons t std::vector<T,
std::allocator< _C
harT&)':
ch3list.cpp:11: error: expected primary-expression before '=' token
ch3list.cpp:11: error: expected primary-expression before '!=' token
ch3list.cpp:11: error: expected primary-expression before ')' token
ch3list.cpp:12: error: expected primary-expression before ')' token

what did I do wrong here?

Many thanks,

-k

Mar 18 '07 #1
7 6475
* ng************@ gmail.com:
Hi,

I have this code

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator i;

for (i = v.begin();i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}

int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
when i tried to compile, it gave

ch3list.cpp: In function `void print_list(cons t std::vector<T,
std::allocator< _C
harT&)':
ch3list.cpp:11: error: expected primary-expression before '=' token
ch3list.cpp:11: error: expected primary-expression before '!=' token
ch3list.cpp:11: error: expected primary-expression before ')' token
ch3list.cpp:12: error: expected primary-expression before ')' token

what did I do wrong here?
Trying to assign to a type.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 18 '07 #2
ng************@ gmail.com wrote:
Hi,

I have this code

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator i;
typename vector<T>::cons t_iterator i;

--
Ian Collins.
Mar 18 '07 #3
ng************@ gmail.com wrote:
Hi,

I have this code

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator i;
Here, you create a typedef for vector::iterato r, and call this type 'i'.
for (i = v.begin();i != v.end(); ++i)
Here, you are trying to assign a value to a type, similar to if you did:

for (int = 0; int != v.size(); ++int)

You need to declare a variable of that type before you can use it.
For clarity, I would call the typedef 'it':

typedef typename vector<T>::iter ator It;
for (It i = v.begin(); i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}

int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
when i tried to compile, it gave

ch3list.cpp: In function `void print_list(cons t std::vector<T,
std::allocator< _C
harT&)':
ch3list.cpp:11: error: expected primary-expression before '=' token
ch3list.cpp:11: error: expected primary-expression before '!=' token
ch3list.cpp:11: error: expected primary-expression before ')' token
ch3list.cpp:12: error: expected primary-expression before ')' token

what did I do wrong here?
See above.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 18 '07 #4
On 18 Mar 2007 14:47:23 -0700 in comp.lang.c++, ng************@ gmail.com
wrote,
> typedef typename vector<T>::iter ator i;

for (i = v.begin();i != v.end(); ++i)
"i" is the name of the type. It is not a variable.
A type cannot appear to the left of =.
You need an actual variable there.

Mar 18 '07 #5
Thanks everyone.

I have tried to change the code as suggested

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator It;

for (It i = v.begin();i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}

int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
but it still flags another error

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: In
constructo
r `__gnu_cxx::__n ormal_iterator< _Iterator,
_Container>::__ normal_iterator (const
__gnu_cxx::__no rmal_iterator<_ Iter, _Container>&) [with _Iter = const
int*, _Ite
rator = int*, _Container = std::vector<int , std::allocator< int]':
ch3list.cpp:11: instantiated from `void print_list(cons t
std::vector<T, std::a
llocator<_CharT &) [with T = int]'
ch3list.cpp:18: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: 609:
error: in
valid conversion from `const int* const' to `int*'

Any ideas?

Many thanks

-k

Mar 19 '07 #6
ng************@ gmail.com wrote:
Thanks everyone.

I have tried to change the code as suggested

#include <vector>
#include <string>
#include <iostream>

using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator It;

for (It i = v.begin();i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}

int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
but it still flags another error
Read my post again and note the type of iterator.

--
Ian Collins.
Mar 19 '07 #7
On Mar 18, 9:48 pm, Ian Collins <ian-n...@hotmail.co mwrote:
nguyen.h.kh...@ gmail.com wrote:
Thanks everyone.
I have tried to change the code as suggested
#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <class Tvoid print_list(cons t vector<T>& v){
typedef typename vector<T>::iter ator It;
for (It i = v.begin();i != v.end(); ++i)
cout << (*i) << " ";
cout << endl;
}
int main(){
vector<int l(5,1);
print_list(l);
return 0;
}
but it still flags another error

Read my post again and note the type of iterator.

--
Ian Collins.- Hide quoted text -

- Show quoted text -
It worked !!!!

Thank you.

-k

Mar 19 '07 #8

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

Similar topics

38
3653
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a...
6
4363
by: greg | last post by:
Hello all, I havent used STL containers much, as I am used to MFC containers/classes always ..The differences that I could see is iterators and algorithms. The algorithms providing some basic functinaloties which are also present in respective MFc container classes, I wanted to know what is the main powers in Iterators ?? I have written...
11
3030
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs using ::pointer? I did a quick experiment and replaced all the ::iterator in my code with ::pointer and it seems to work the same. What I'm think...
1
1919
by: Dom Gilligan | last post by:
What exactly can you assign to a set iterator, by assignment or initialisation? (is this covered in Josuttis? I can't find it). Some (limited) digging around in the Gnu code shows no operator=, but 2 ctors for iterators, and 3 for const_iterators. For const_iterator, there's a no-parameter ctor, and a ctor which takes another...
8
2438
by: Mateusz Łoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
14
4865
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to...
27
5303
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
3
4504
by: vasili | last post by:
hello All, I have a simple issue. I defined a custom container, that encloses a std::list, which in turn holds objects that are a simple abstraction of a six position array. Now, i would like to serialize the whole newly-defined container, in order to copy the contents to another array. So i thought to define an iterator which...
16
6062
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last element in the container? Or is there a cleaner way of getting an iterator to the last element?
2
4256
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7839
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...
0
8200
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. ...
0
8338
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...
1
7954
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...
1
5710
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...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.