Iterator-Template problem

N

nguyen.h.khanh

Hi,

I have this code

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

using namespace std;


template <class T> void 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
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,

I have this code

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

using namespace std;


template <class T> void 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.
 
I

Ian Collins

Hi,

I have this code

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

using namespace std;


template <class T> void print_list(const vector<T>& v){
typedef typename vector<T>::iterator i;
typename vector<T>::const_iterator i;
 
M

Marcus Kwok

Hi,

I have this code

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

using namespace std;


template <class T> void 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':

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

David Harmon

On 18 Mar 2007 14:47:23 -0700 in comp.lang.c++, (e-mail address removed)
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.
 
N

nguyen.h.khanh

Thanks everyone.

I have tried to change the code as suggested

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

using namespace std;


template <class T> void 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
 
I

Ian Collins

Thanks everyone.

I have tried to change the code as suggested

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

using namespace std;


template <class T> void 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.
 

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,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top