Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do I make this work c + + , the assignment is to make raw pointers into smart pointers - the error is c

how do I make this work c++, the assignment is to make raw pointers into smart pointers- the error is c2280 on line 73(iterator& operator++() function
#pragma once
#include
#include
#include
using namespace std;
template
class List {
private:
class Node {
public:
T data;
unique_ptr prev;
unique_ptr next;
bool isHiddenNode = false;
};
unique_ptr head;
unique_ptr tail;
public:
class const_iterator {
protected:
unique_ptr current;
T& retrieve() const { return current->data; }
const_iterator(unique_ptr p) : current(p){}
friend class List;
public:
explicit const_iterator() : current(nullptr){}
T& operator*() const {
return retrieve();
}
const_iterator& operator++(){
current = current->next;
return *this;
}
const_iterator operator++(int){
const_iterator old =*this;
++(*this);
return old;
}
bool operator==(const const_iterator& rhs) const {
return current == rhs.current;
}
bool operator!=(const const_iterator& rhs) const {
return !(*this == rhs);
}
};
public:
class iterator : public const_iterator {
protected:
iterator(unique_ptr p) : const_iterator(p){}
friend class List;
public:
explicit iterator(){}
T& operator *(){
return const_iterator::retrieve();
}
const T& operator*() const {
return const_iterator::operator*();
}
iterator& operator++(){
this->current = const_iterator::current->next;
return *this;
}
iterator operator++(int){
iterator old =*this;
++(*this);
return old;
}
};
private:
void setupList(){
unique_ptr newNode = new Node();
newNode->next = tail;
newNode->prev = head;
head->next = newNode;
tail->prev = newNode;
}
void deleteListContents(){
unique_ptr current = head->next;
unique_ptr temp = nullptr;
while (current != tail->prev){
temp = current->next;
delete current;
current = temp;
}
}
public:
List(){
head = new Node();
head->isHiddenNode = true;
tail = new Node();
tail->isHiddenNode = true;
head->prev = nullptr;
head->next = tail;
tail->prev = head;
tail->next = nullptr;
};
List(T newData){
setupList();
(head->next)->data = newData;
}
explicit List(List& rhs){
// copy constructor
deleteListContents();
head = rhs.head;
tail = rhs.tail;
}
virtual ~List(){
// And a destructor
deleteListContents();
// deleteListContents() leaves the head pointer, so explicit
// delete req'd
delete head;
}
bool empty(){
return (head->next == tail);
}
// iterator related methods
iterator begin(){ return { head->next }; }
iterator end(){ return tail; }
const_iterator cbegin() const {
return { head->next };
}
const_iterator cend() const
{
return { tail };
}
iterator erase(iterator itr){
unique_ptr p = itr.current;
iterator iterToReturn{ p->next };
p->prev->next = p->next;
p->next->prev = p->prev;
return iterToReturn;
}
iterator insert(iterator itr, const T& x){
unique_ptr p = itr.current;
unique_ptr newNode = new Node{ x, p->prev, p };
p->prev = p->prev->next = newNode;
}
iterator erase(iterator from, iterator to){
iterator itr = from;
while (itr != to){
itr = erase(itr);
}
return to;
}
// And the methods for the rest
void push_front(T data){
if (this->empty()){
setupList();
unique_ptr actualHead = head->next;
actualHead->data = data;
}
else {
unique_ptr actualHead = head->next;
unique_ptr newNode = new Node();
newNode->data = data;
newNode->next = actualHead;
actualHead->prev = newNode;
newNode->prev = head;
head->next = newNode;
}
}
void push_back(T data){
if (this->empty()){
setupList();
unique_ptr actualTail = tail->prev;
actualTail->data = data;
}
else {
unique_ptr actualTail = tail->prev;
unique_ptr newNode = new Node();
newNode->data = data;
newNode->prev = actualTail;
actualTail->next = newNode;
newNode->next = tail;
tail->prev = newNode;
}
}
T front(){
unique_ptr actualHead = head->next;
return (actualHead->data);
}
T back(){
unique_ptractualTail = tail->prev;
return (actualTail->data);
}
void pop_back(){
if (!empty()){
unique_ptr lastNode = tail->prev;
tail->prev = lastNode->prev;
unique_ptrnewLastNode = tail->prev;
newLastNode->next = tail;
delete lastNode;
lastNode = nullptr;
}
else {
std::cerr << "pop_back(): Attempt to pop from empty list. "<< std::endl;
}
}
void pop_front(){
if (!empty()){
unique_ptr firstNode = head->next;
head->next = firstNode->next;
unique_ptr newFirstNode = head->next;
newFirstNode->prev = head;
delete firstNode;
firstNode = nullptr;
}
else {
std::cerr << "pop_back(): Attempt to pop from empty list. "<< std::endl;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions