Parse Errors...newbie help

B

Barry Hynes

Hi Folks,

why does the following code give me parse errors,

What i am trying to do is create an operator= for safelist.
keep getting parse errors when i try and use the iterators in list<T>.

any help greatly appreciated

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>


template<typename T>
class SafeList : private list<T> {
public:
using list<T>::push_front;
using list<T>::size;
using list<T>::insert;
using list<T>::begin;
using list<T>::end;
//using list<T>::const_iterator;
//using list<T>::iterator;

//constructors
SafeList() : list<T>(){}//No no-arg ctor
SafeList(const T* first, const T* last) : list<T>(){
insert(begin(), *first, *last);
}

SafeList(const SafeList<T>& x) : list<T>(){
insert(begin(), x.begin(), x.end());
}

//destructor
~SafeList(){
erase(begin(), end());
}

//assignment
SafeList<T>& operator=(const SafeList<T>& x){
if(this != &x){
list<T>::iterator f1; //error
f1 = begin();
list<T>::const_iterator l1 //error
l1 = end();
list<T>::const_iterator f2 //error
f2 = x.begin();
list<T>::const_iterator l2 //error
l2 = x.end();
while(f1 != l1 && f2 != l2)
*f1++ = *f2++;
if (f2 == l2)
erase(f1, l1);
else
insert(l1, f2, l2);
}
return *this;
}
............
 
V

Victor Bazarov

Barry said:
Hi Folks,

why does the following code give me parse errors,

What i am trying to do is create an operator= for safelist.
keep getting parse errors when i try and use the iterators in list<T>.

any help greatly appreciated

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>


template<typename T>
class SafeList : private list<T> {

A hint: inside the template definition of 'SafeList', you don't have to
write the said:
[...]
//assignment
SafeList<T>& operator=(const SafeList<T>& x){

if(this != &x){
list<T>::iterator f1; //error

You will need to say

typename list said:
f1 = begin();

And always prefer initialisation over assignment.
 

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

Forum statistics

Threads
474,159
Messages
2,570,885
Members
47,419
Latest member
ArturoBres

Latest Threads

Top