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 7 6470
* 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? 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. 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
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.
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 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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=,...
|
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 <---|
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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 projectplanning, coding, testing,...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |