iterator problem | Newbie | | Join Date: Mar 2007
Posts: 7
| | |
Hi everyone,
I am a newbie to C++ programming and am running into iterator problems..here are the snippets and the error:
//First, I have a template class called SimpleList that provides linked list functionality with functions like push, pop etc..The idea is to create stack, queues of diff. types (like char, int etc.)
~~bunch of code in between~
//function to search a list for a particular SimipleList object
217 template <class type> string srch_list(list<SimpleList<type> *>
. &list_name, string srch_name)
. {
. string condition;
. typename list<type>::const_iterator iter;
221 for(iter = list_name.begin(); iter != list_name.end(); iter++)
. {
. if (list_name->retn_name() == srch_name)
. condition = "true";
}
return condition;
227 }
//Near the end of the program, I have made a linked list of pointers to SimpleList of objects eg. there is a linked list with pointers to SimpleList objects(eg. of type int)
list<SimpleList<int> *> listSLi;
~code~
//I am calling the function with the line
string condition2;
312 condition2 = srch_list(listSLi, name_stq); //name_stq is the name of stack
I want to search
//I push SimpliList objects into listSLi later on..
But I get the error
ds_projectfinal.cpp: In function `std::string srch_list(std::list<SimpleList<typ
e>*, std::allocator<SimpleList<type>*> >&, std::string) [with type = int]':
ds_projectfinal.cpp:312: instantiated from here
ds_projectfinal.cpp:221: error: no match for 'operator=' in 'iter = (+list_name)
->std::list<_Tp, _Alloc>::begin [with _Tp = SimpleList<int>*, _Alloc = std::allo
cator<SimpleList<int>*>]()'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_list.h:188: note: candida
tes are: std::_List_const_iterator<int>& std::_List_const_iterator<int>::operato
r=(const std::_List_const_iterator<int>&)
ds_projectfinal.cpp:312: instantiated from here
ds_projectfinal.cpp:221: error: no match for 'operator!=' in 'iter != (+list_nam
e)->std::list<_Tp, _Alloc>::end [with _Tp = SimpleList<int>*, _Alloc = std::allo
cator<SimpleList<int>*>]()'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_list.h:254: note: candida
tes are: bool std::_List_const_iterator<_Tp>::operator!=(const std::_List_const_
iterator<_Tp>&) const [with _Tp = int]
I would be grateful if someone could enlighten me with some C++ light...
| | Newbie | | Join Date: Mar 2007
Posts: 7
| | | re: iterator problem
I changed the iterator to
template <class type> string srch_list(list <type> &list_name, std::string srch_name)
{
string condition;
typename list<type>::const_iterator iter;
for(iter = (*list_name).begin(); iter != (*list_name).end(); iter++)
{
if ((*list_name)->retn_name() == srch_name)
condition = "true";
}
return condition;
}
//with the function being called as
condition = srch_list(listSLi, name_stq);
so, the previous error is gone but there is something else..something more sinister (joking)... but something I cannot make sense of:
It gives me the following error on compiling--
/cygdrive/c/DOCUME~1/Shrestha/LOCALS~1/Temp/ccfdnGQN.o:ds_projectfinal.cpp:(.tex
t+0x9a8): undefined reference to `std::basic_string<char, std::char_traits<char>
, std::allocator<char> > srch_list<int>(std::list<SimpleList<int>*, std::allocat
or<SimpleList<int>*> >&, std::basic_string<char, std::char_traits<char>, std::al
locator<char> >)'
/cygdrive/c/DOCUME~1/Shrestha/LOCALS~1/Temp/ccfdnGQN.o:ds_projectfinal.cpp:(.tex
t+0xdf5): undefined reference to `std::basic_string<char, std::char_traits<char>
, std::allocator<char> > srch_list<int>(std::list<SimpleList<int>*, std::allocat
or<SimpleList<int>*> >&, std::basic_string<char, std::char_traits<char>, std::al
locator<char> >)'
collect2: ld returned 1 exit status
Does not anyone have an idea what is going on?
| | Newbie | | Join Date: Mar 2007
Posts: 7
| | | re: iterator problem
I changed the above snippet of code to
template <class type>
string srch_list(std::list <type> &list_name, std::string srch_name)
{
string condition;
typename list<type>::const_iterator iter;
for(iter = list_name.begin(); iter != list_name.end(); iter++)
{
if (iter->retn_name() == srch_name)
condition = "true";
}
return condition;
}
//retn_name is a public function in SimpleList base class that returns the private //data member that stores the name of the SimpleList.
//And now the error is:
ds_projectfinal.cpp: In function `std::string srch_list(std::list<type, std::all
ocator<_CharT> >&, std::string) [with type = SimpleList<int>*]':
ds_projectfinal.cpp:340: instantiated from here
ds_projectfinal.cpp:241: error: request for member `retn_name' in `*(&iter)->std
::_List_const_iterator<_Tp>::operator-> [with _Tp = SimpleList<int>*]()', which
is of non-class type `SimpleList<int>* const'
I am having fun talking to myself, but it would be more enjoyable if someone joined me....
If someone could point me in the right direction, it would be really appreciated.
| | Newbie | | Join Date: Mar 2007
Posts: 7
| | | re: iterator problem
forgot to mention:
line 340 condition = srch_list(listSLi, name_stq);//which calls the function
~code~
template <class type>
string srch_list(std::list <type> &list_name, std::string srch_name)
{
string condition;
typename list<type>::const_iterator iter;
for(iter = list_name.begin(); iter != list_name.end(); iter++)
{
linen 241 if (iter->retn_name() == srch_name)
condition = "true";
}
return condition;
}
|  | Expert | | Join Date: Mar 2007 Location: Chennai
Posts: 1,258
| | | re: iterator problem
HI,
I went through ur code.
The problem is u ra ecalling the function (iter->retn_name() == srch_name)
I think this cant be be done..
Try to access the value directly and i think ur problem will be solved.
Thanks
Raghuram
| | Newbie | | Join Date: Mar 2007
Posts: 7
| | | re: iterator problem
Thanx gpraghuram,
Well, the problem was that I was trying to access retn_name in iter but lo and behold iter is a pointer and it doesn't have retn_name. What iter points to has the function so I changed the iter in line 241 to (*iter) -> retn_name and everything is fine. By fine I mean it compiles, it still doesn't do what I want it to.
Anyway, thanx to everyone for helping sp. gpraghuram....
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|