By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

Iterator-Template problem

P: n/a
Hi,

I have this code

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

using namespace std;
template <class Tvoid print_list(const vector<T>& v){
typedef typename vector<T>::iterator 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(const 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
Share this Question
Share on Google+
7 Replies


P: n/a
* ng************@gmail.com:
Hi,

I have this code

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

using namespace std;
template <class Tvoid print_list(const vector<T>& v){
typedef typename vector<T>::iterator 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(const 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

P: n/a
ng************@gmail.com wrote:
Hi,

I have this code

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

using namespace std;
template <class Tvoid print_list(const vector<T>& v){
typedef typename vector<T>::iterator i;
typename vector<T>::const_iterator i;

--
Ian Collins.
Mar 18 '07 #3

P: n/a
ng************@gmail.com wrote:
Hi,

I have this code

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

using namespace std;
template <class Tvoid print_list(const vector<T>& v){
typedef typename vector<T>::iterator i;
Here, you create a typedef for vector::iterator, 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>::iterator 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(const 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

P: n/a
On 18 Mar 2007 14:47:23 -0700 in comp.lang.c++, ng************@gmail.com
wrote,
> typedef typename vector<T>::iterator 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

P: n/a
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(const vector<T>& v){
typedef typename vector<T>::iterator 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::__normal_iterator<_Iterator,
_Container>::__normal_iterator(const
__gnu_cxx::__normal_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(const
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

P: n/a
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(const vector<T>& v){
typedef typename vector<T>::iterator 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

P: n/a
On Mar 18, 9:48 pm, Ian Collins <ian-n...@hotmail.comwrote:
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(const vector<T>& v){
typedef typename vector<T>::iterator 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 discussion thread is closed

Replies have been disabled for this discussion.