rogger123
Advanced Member level 4
Hi,
I have a problem compiling the following piece of code using reverse itretor for a list.
#include <iostream>
#include <list>
using namespace std;
// Display elements in reverse order
template <class T>
void displayListRev(const list<T> &lst)
{
list<T>::reverse_iterator rl;
for(rl = lst.rbegin(); rl < lst.rend(); ++rl)
cout << *rl << ' ';
cout << endl;
}
int main()
{
list<int> listType;
listType.push_back(10);
listType.push_back(30);
listType.sort();
cout << "List contents in reverse order: ";
displayListRev(listType);
return 0;
}
it gives me errors for the "for" loop. one of the error is pasted below:
"binary '=' : no operator defined which takes a right-hand operand of type 'class std::reverse_bidirectional_iterator<class std::list<int,class std::allocator<int> >::const"
If I get rid of the function 'displayListRev(listType)' and instead add the correcponding function code within the main function, the error gets resolved and correct output is generated.
Can someone explain the reason for this behaviour.
The modified code is as shown below:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> listType;
list<int>::reverse_iterator rl;
listType.push_back(50);
listType.push_back(30);
listType.sort();
cout << "List contents in reverse order: ";
for(rl = listType.rbegin(); rl != listType.rend(); ++rl)
cout << *rl << ' ';
return 0;
}
regards
rogger123
I have a problem compiling the following piece of code using reverse itretor for a list.
#include <iostream>
#include <list>
using namespace std;
// Display elements in reverse order
template <class T>
void displayListRev(const list<T> &lst)
{
list<T>::reverse_iterator rl;
for(rl = lst.rbegin(); rl < lst.rend(); ++rl)
cout << *rl << ' ';
cout << endl;
}
int main()
{
list<int> listType;
listType.push_back(10);
listType.push_back(30);
listType.sort();
cout << "List contents in reverse order: ";
displayListRev(listType);
return 0;
}
it gives me errors for the "for" loop. one of the error is pasted below:
"binary '=' : no operator defined which takes a right-hand operand of type 'class std::reverse_bidirectional_iterator<class std::list<int,class std::allocator<int> >::const"
If I get rid of the function 'displayListRev(listType)' and instead add the correcponding function code within the main function, the error gets resolved and correct output is generated.
Can someone explain the reason for this behaviour.
The modified code is as shown below:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> listType;
list<int>::reverse_iterator rl;
listType.push_back(50);
listType.push_back(30);
listType.sort();
cout << "List contents in reverse order: ";
for(rl = listType.rbegin(); rl != listType.rend(); ++rl)
cout << *rl << ' ';
return 0;
}
regards
rogger123