I can't understand why I'm getting a conversion error on my Node ctor, the node constructor accepts 'const T&' as the first parameter and thats what my insert method is passing into the constructor but for some reason its still throwing me that error.
error C2440: 'initializing' : cannot convert from 'const Record' to 'SortedList<>::Node *
SortedList is of type 'Record' but if I type anything inbetween <> everything in between the brackets will disappear.
List class:
class SortedList{
struct Node{
T data_;
Node* next_;
Node* prev_;
Node(const T& data=T{},Node* nx=nullptr,Node* pr=nullptr){
data_ = data;
next_ = nx;
prev_ = pr;
}
};
Node* front_;
Node* back_;
public:
class const_iterator{
friend class SortedList;
protected:
Node* curr_;
const_iterator(Node* n){
curr_ = n;
}
public:
const_iterator(){
curr_ = nullptr;
}
const_iterator operator++(){
curr_ = curr_->next_;
return *this;
}
const_iterator operator++(int){ //advances curr returns old
const_iterator old = *this;
curr_ = curr_->next_;
return old;
}
const_iterator operator--(){
curr_ = curr_->prev_;
return *this;
}
const_iterator operator--(int){
const_iterator temp = *this;
curr_ = curr_->prev_;
return temp;
}
bool operator==(const_iterator rhs){
return curr_->data_ == rhs.curr_->data_;
}
bool operator!=(const_iterator rhs){
return curr_->data_ != rhs.curr_->data_;
}
bool operator<(const_iterator rhs){
return curr_->data_ < rhs.curr_->data_;
}
const T& operator*()const{
return curr_->data_;
}
};
Where the conversion error is being throw:
template <typename T>
typename SortedList<T>::iterator SortedList<T>::insert(const T& data){
Node* n(data);
iterator it_temp(n);
iterator sort(front_);
while (sort < it_temp){
++sort;
}
n.next_ = sort.curr_;
n.prev_ = sort.curr_->prev_;
sort.curr_->prev_->next_ = n;
sort.curr_->prev_ = n;
}
The error is specifically being throw where Node* n is being constructed in the insert function.
You cannot construct a pointer like this
Node* n(data);
You'd have to, for example, use new
Node* n = new Node(data);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I have made this doubly linked list class in C++ , it utilises sentinel nodes as well in it , I was making an insert(iterator it , T& value) function in the list , which inserts a new node before the node that is specified by the iterator it . I was testing the code , it wasn't working at the end cases i.e when the iterator had its value as the starting node and the ending node
Doubly Linked List class
template <typename T>
class DoubleList
{
struct Node
{
Node *prev;
Node *next;
T data;
Node(const T &data = T{}, Node *next = nullptr, Node *prev = nullptr)
{
this->data = data;
this->next = next;
this->prev = prev;
}
};
Node *front;
Node *back;
int size;
public:
class const_iterator
{
public:
const_iterator() {}
const_iterator &operator++() {}
const_iterator operator++(int) {}
const_iterator &operator--() {}
const_iterator operator--(int) {}
bool operator==(const_iterator rhs) {}
bool operator!=(const_iterator rhs) {}
const T &operator*() const {}
};
class iterator : public const_iterator
{
public:
iterator() {}
iterator &operator++() {}
iterator operator++(int) {}
iterator &operator--() {}
iterator operator--(int) {}
T &operator*() {}
const T &operator*() const {}
};
iterator begin()
{
return iterator(this->front->next, this);
}
iterator end()
{
return iterator(this->back, this);
}
const_iterator cbegin() const
{
return const_iterator(this->front->next, this);
}
const_iterator cend() const
{
return const_iterator(this->back, this);
}
Constructor for the linked list
template <typename T>
DoubleList<T>::DoubleList()
{
front = new Node();
back = new Node();
front->next = back;
back->prev = front;
}
template <typename T>
DoubleList<T>::~DoubleList()
{
Node *curr = this->front;
while (this->curr != back)
{
Node *next = curr->next;
delete curr;
curr = next;
}
delete this->front;
delete this->back;
this->size = 0;
}
iterator function
class iterator : public const_iterator
{
friend class DoubleList;
iterator(Node *n, const DoubleList *linkedList)
{
const_iterator(n, linkedList);
}
public:
iterator()
{
const_iterator();
}
iterator &operator++()
{
if (this->curr)
{
this->curr = this->curr->next;
}
return *this;
}
iterator operator++(int)
{
iterator old = *this;
if (this->curr)
{
this->curr = this->curr->next;
}
return old;
}
iterator &operator--()
{
if (this->curr)
{
this->curr = this->curr->prev;
}
else
{
this->curr = this->list->back;
}
return *this;
}
iterator operator--(int)
{
iterator old = *this;
if (this->curr)
{
this->curr = this->curr->prev;
}
else
{
this->curr = this->list->back;
}
return old;
}
T &operator*()
{
return this->curr->data;
}
const T &operator*() const
{
return this->curr->data;
}
Insert() function
template <typename T>
typename DoubleList<T>::iterator DoubleList<T>::insert(iterator it, const T &data)
{
Node *nn;
if (front->next != back)
{
nn = new Node(data, it.curr, it.curr->prev);
it.curr->prev->next = nn;
it.curr->prev = nn;
}
else
{
nn = new Node(data, back, front);
front->next = nn;
back->prev = nn;
}
this->size_++;
return iterator(nn, this);
}
the error occurs when i create a new list and insert a node into it
when i check wether end() and begin() point to different nodes in the linkedList ,it points to the same node , meaning that the node was never added into the list , what can i correct here to make it work fine?
a minimal reproducible example can be done by the following code
int main(){
DoubleList<int> theList;
DoubleList<int>::iterator it1 = theList.insert(theList1.begin(), 1);
if (theList.begin() == theList.end())
{
std::cout<<"Error recieved"<<std::endl;
}
}
I'm trying to write a class similar to std::list in C++.
It's my first time dealing with templated classes and I get this weird error:
C2447: '{' missing function header (old-style formal list?)
Here is the code snippet:
template<typename item>
mylist<item>::iterator mylist<item>::erase(iterator pos)
{
iterator cursor(head);
while (cursor->next != (*pos)) ++cursor;
if ((*cursor) == nullptr || (*cursor) == head) return iterator(nullptr);
m_node* tmp = cursor->next->next;
delete cursor->next;
cursor->next = tmp;
--m_size;
return tmp;
}
The class definition:
template<typename item>
class mylist
{
class m_node
{
friend class mylist;
item* node_data;
m_node* next;
public:
m_node()
{
next = nullptr;
node_data = nullptr;
}
m_node(const item& ITEM): m_node()
{
node_data = new item(ITEM);
}
~m_node() { delete node_data; delete next; }
};
m_node* head;
m_node* tail;
unsigned int m_size;
public:
class iterator
{
friend class mylist;
m_node* m_ptr;
public:
iterator()
{
m_ptr = nullptr;
}
iterator(m_node* i_ptr)
{
m_ptr = i_ptr;
}
iterator(const iterator& other)
{
m_ptr = other.m_ptr;
}
~iterator(){}
const iterator& operator=(const iterator& other) const
{
m_ptr = other.m_ptr;
return this;
}
iterator& operator++()
{
++m_ptr;
return *this;
}
item operator->()
{
return *(m_ptr->node_data);
}
item operator*()
{
return *(m_ptr->node_data);
}
bool operator!=(const iterator& other) { return m_ptr != other.m_ptr; }
bool operator ==(const iterator& other) { return m_ptr == other.m_ptr; }
};
mylist()
{
head = tail = new m_node();
m_size = 0;
}
~mylist()
{
delete head;
}
bool isempty() const
{
return head == tail;
}
const iterator& push_back(const item& i_item)
{
tail->next = new m_node(i_item);
tail = tail->next;
++m_size;
return iterator(tail);
}
iterator begin() const
{
return iterator(head);
}
iterator end() const
{
return nullptr;
}
item& back()
{
return *(tail->node_data);
}
unsigned int size() const
{
return m_size;
}
iterator erase(iterator pos);
void remove(item T);
};
The error occurs at the first curly of the function's scope.
I have read some documentations regarding this error and templated classes but could find what seems to be the error.
As mentioned in the comments, you will need a typename preceding the declared return type of the function template. This extra 'hint' for the compiler is required in this case because that return type is dependent on the item type; and, quoting from that linked cppreference page, it is "a compound type constructed from a dependent type".
template<typename item>
typename mylist<item>::iterator mylist<item>::erase(iterator pos)
{
iterator cursor{ head };
while (cursor->next != (*pos)) ++cursor;
if ((*cursor) == nullptr || (*cursor) == head) return iterator(nullptr);
m_node* tmp = cursor->next->next;
delete cursor->next;
cursor->next = tmp;
--m_size;
return tmp;
}
Also note that I have changed the parentheses to curly braces in the iterator cursor{ head }; line; this is now generally accepted as better style, to avoid possible confusion between object initializers and function declarations.
Code below, for my linked list implementation and specifically emplace_back below. What am I doing wrong and how to fix?
Error I am getting is:
Build started...
1>------ Build started: Project: so_list_emplace, Configuration: Debug Win32 ------
1>main.cpp
1list.hpp(191,19): error C2280: 'list<President>::node::node(void)': attempting to reference a deleted function
1>list.hpp(15): message : compiler has generated 'list<President>::node::node' here
1>list.hpp(15,1): message : 'list<President>::node::node(void)': function was implicitly deleted because a data member 'list<President>::node::value' has either no appropriate default constructor or overload resolution was ambiguous
1>list.hpp(12): message : see declaration of 'list<President>::node::value'
1>main.cpp(29): message : see reference to function template instantiation 'void list<President>::emplace_back<const char(&)[15],const char(&)[13],int>(const char (&)[15],const char (&)[13],int &&)' being compiled
1>main.cpp(29): message : see reference to function template instantiation 'void list<President>::emplace_back<const char(&)[15],const char(&)[13],int>(const char (&)[15],const char (&)[13],int &&)' being compiled
1>list.hpp(192,10): error C2679: binary '=': no operator found which takes a right-hand operand of type 'T *' (or there is no acceptable conversion)
1> with
1> [
1> T=President
1> ]
1>main.cpp(22,13): message : could be 'President &President::operator =(const President &)'
1>list.hpp(190,1): message : while trying to match the argument list '(T, T *)'
1> with
1> [
1> T=President
1> ]
1>Done building project "so_list_emplace.vcxproj" -- FAILED.
list.hpp implementation:
#ifndef LIST_HPP_
#define LIST_HPP_
#include <cstddef>
#include <initializer_list>
#include <utility>
template< typename T >
class list {
public:
struct node {
T value;
node* next;
node* prior;
};
struct iterator {
iterator(node* nod) : ptr_(nod) {}
iterator& operator++() {
if (ptr_) {
ptr_ = ptr_->next;
}
return *this;
}
iterator operator++(T) {
auto old = *this;
if (ptr_) {
ptr_ = ptr_->next;
}
return old;
}
T& operator*() const { return ptr_->value; }
T* operator->() { return &ptr_->value; }
bool operator==(const iterator& other) { return ptr_ == other.ptr_; }
bool operator!=(const iterator& other) { return ptr_ != other.ptr_; }
node* ptr_;
};
list() : head_(nullptr), tail_(nullptr), size_(0) {}
// O(n)
template< typename input_iterator >
list(input_iterator first, input_iterator last) : head_(nullptr), tail_(nullptr), size_(0) {
for (auto it = first; it != last; ++it) {
push_back(*it);
}
}
// O(n)
list(std::initializer_list<T> init) : list<T>(init.begin(), init.end()) {}
// O(n)
list(const list& other) : head_(nullptr), tail_(nullptr), size_(0) {
auto it = other.begin();
while (it != nullptr) {
push_back(*it);
++it;
}
size_ = other.size();
}
// O(n)
list& operator=(const list& other) {
if (this != &other) {
clear();
auto it = other.begin();
while (it != nullptr) {
push_back(*it);
++it;
}
size_ = other.size();
}
return *this;
}
list(list&& other) : head_(other.head_), tail_(other.tail_), size_(other.size()) {
other.size_ = 0;
other.head_ = nullptr;
other.tail_ = nullptr;
}
list& operator=(list&& other) {
head_ = other.head_;
tail_ = other.tail_;
size_ = other.size();
other.clear();
other.head_ = nullptr;
other.tail_ = nullptr;
other.size_ = 0;
}
// O(n)
~list() {
clear();
}
// O(n)
void clear() {
if (head_) {
node* current = head_;
while (current) {
node* next = current->next;
delete current;
current = next;
}
}
head_ = nullptr;
tail_ = nullptr;
size_ = 0;
}
// O(1)
bool empty() const {
return head_ == nullptr;
}
// O(1)
void push_back(const T& value) {
node* newnode = make_node(value);
if (tail_) {
node* oldtail = tail_;
oldtail->next = newnode;
newnode->prior = oldtail;
tail_ = newnode;
}
else {
head_ = tail_ = newnode;
}
++size_;
}
// O(1)
size_t size() const {
return size_;
}
iterator begin() {
return iterator(head_);
}
const iterator begin() const {
return iterator(head_);
}
iterator end() {
return nullptr;
}
const iterator end() const {
return nullptr;
}
// O(1)
T& front() { return *iterator(head_); }
const T& front() const { return *iterator(head_); }
// O(1)
T& back() { return *iterator(tail_); }
const T& back() const { return *iterator(tail_); }
// O(1)
void pop_back() {
if (tail_) {
node* newtail = tail_->prior;
if (newtail) {
newtail->next = nullptr;
}
else {
// means that head_ has also been erased
head_ = nullptr;
}
delete tail_;
tail_ = newtail;
--size_;
}
}
template<typename... P>
void emplace_back(P&&... v)
{
node* newnode = new node;
newnode->value = new T(std::forward<P>(v)...);
newnode->next = nullptr;
newnode->prior = nullptr;
if (tail_) {
node* oldtail = tail_;
oldtail->next = newnode;
newnode->prior = oldtail;
tail_ = newnode;
}
else {
head_ = tail_ = newnode;
}
++size_;
}
private:
node* make_node(const T& value) {
node* newnode = new node;
newnode->value = value;
newnode->next = nullptr;
newnode->prior = nullptr;
return newnode;
}
node* head_;
node* tail_;
size_t size_;
};
#endif // LIST_HPP_
main.cpp to exercise:
#include "list.hpp"
#include <iostream>
#include <string>
struct President
{
std::string name;
std::string country;
int year;
President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
President(President&& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year)
{
std::cout << "I am being moved.\n";
}
President& operator=(const President& other) = default;
};
int main() {
list<President> elections;
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
}
Your issue is that node is trying to construct a President, but President doesn't have a default constructor. You'll need to construct a node with a President, like in this constructor:
node(T const &v) : value(v) { }
Because emplace_back tries to construct an object in place, you'll also need to add a constructor to construct value using the arguments that are passed in.
template <typename ...Args>
node(Args &&...v) : value(std::forward<Args>(v)...) { }
Lastly, update your emplace_back function to forward arguments at construction time.
node* newnode = new node(std::forward<P>(v)...);
This is compiling for me with gcc and clang.
The problems(as reflected in the errors) are:
There is no default constructor for list::node::node() because it is implicitly deleted.
President has no default constructor because you've user defined constructors for President and so compiler will not synthesize the default constructor for you.
There is no operator= for President.
To solve these problems you need to add the following things:
Default constructor for node like node() = default(); inside node.
Default constructor for President like President() = default; inside President.
Define(overload) the assignment operator for President.
Consider a standard implementation of class Link of LinkedList in c++.
I want to know if its a good idea to overload the operator ++ in this class (I noticed that I repeat the line link = link->next; a lot when dealing with linked lists, so I thought it would be easier if I overload this operator). Here's my implementation:
#ifndef LINK_H
#define LINK_H
#include <iostream>
#include "typeinfo.h"
#include "LinkedList.h"
template <class T> class LinkedList; //|Forward declaration of the generic LinkedList.
template <class T>
class Link
{
public:
//|-------------------- Constructors --------------------
Link(T data): m_data(data), next(NULL){}
//|-------------------- Methods --------------------
T getData(){
return m_data;
}
T& getNext(){
return next;
}
void setNext(Link* newLink){
next = newLink;
}
void setData(T data){
m_data = data;
}
//|-------------------- Operator overload --------------------
bool operator==(Link& other){
if(this->m_data == other.m_data)
return true;
return false;
}
void operator++(){ //Is this okay?
this = this->next;
}
//|-------------------- Friend functions --------------------
friend std::ostream& operator<<(std::ostream& out,const Link<T>& link){
out<<link.m_data;
return out;
}
//|-------------------- Destructor --------------------
virtual ~Link(){}
protected:
public:
//|-------------------- Private fields --------------------
T m_data;
Link<T>* next;
friend class LinkedList<T>;
};
#endif // LINK_H
I guess the way that I tried to do it is not good (it does work as I expected). I tried to use this because I want it to work on pointer that is pointing to a certain link.
So, is it a good idea? if it is, what is the right way to implement it?
Thanks.
Maybe you should refactor your design and the code.
The link, or better said the Node, is normally implemented as an own class. An this class is embedded in the LinkedList class. And that Node class should be completely encapsulated and not shown to the outside world.
The user of the class will just deal with the data value. All the pointer stuff should be hidden.
And to be able to iterate over your class, which is similar to a std::forward_list, you can add ultra simple iterator functionality. Most functions can be implemented using a one liner.
I will show you below an ultra simple implementation example. This may be extended easily according to your needs.
From that you may take over some ideas to improve your design.
With the added iterator functionality, you may use range based for loops and std:: algorithms and all the like.
Please have a look and ask questions, if there should be something unclear.
#include <iostream>
#include <iterator>
#include <initializer_list>
#include <algorithm>
// Very simple implementation of a forward list
template <class T>
class SinglyLinkedList {
// The node
struct Node {
T data{}; // Data. Would normally be a templated argument
Node* next{}; // And the pointer to the next node
Node(const T& i, Node* n = nullptr) : data(i), next(n) {}; // Simple constructor to set a value and next pointer
};
Node* head{}; // This is the start of the list
// It would be advisable to have a tail pointer. We use the more inefficient approach here
Node* getLast() const { Node* n{ head }; while (n and n->next) n = n->next; return n; }
public:
// Constructor / Destructor --------------------------------------------------------------------------------------------------------
~SinglyLinkedList() { clear(); }
// Default constuctor
SinglyLinkedList() {} // Default
// From an initialization list
SinglyLinkedList(const std::initializer_list<T>& il) { clear(); for (const T& i : il) push_back(i); } // From initializer list
// Copy constructor
SinglyLinkedList(const SinglyLinkedList& other) { clear(); for (const T &i : other) push_back(i); }
// Move constructor. Will steal the elements from the other
SinglyLinkedList(SinglyLinkedList&& other) noexcept { head = other.head; other.head = nullptr; }
// Assignment operator
SinglyLinkedList& operator = (const SinglyLinkedList& other) { clear(); for (const T &i : other) push_back(i); }
// Move assignment operator
SinglyLinkedList& operator = (SinglyLinkedList&& other) { head = other.head; other.head = nullptr; }
// Housekeeping --------------------------------------------------------------------------------------------------------------
void clear() { Node* tmp{ head }; while (tmp) { Node* toDelete{ tmp }; tmp = tmp->next; delete toDelete; } head = nullptr; }
int empty() const { return head == nullptr; }
int size() const { int k{}; Node* n{ head }; while (n) { ++k; n = n->next; } return k; }
// Modify content --------------------------------------------------------------------------------------------------------------
void push_front(const T& i) { Node* n = new Node(i); n->next = head; head = n; };
void push_back(const T& i) { Node* n = new Node(i); Node* l = getLast(); if (l) l->next = n; else head = n; }
void pop_front() { if (head) { Node* tmp = head->next; delete head; head = tmp; } }
void pop_back() { // This is a little bit more difficult in a singly linked list
if (head) {
Node* n{ head }, * previous{};
while (n and n->next) {
previous = n;
n = n->next;
}
delete n;
if (previous)
previous->next = nullptr;
else
head->next = nullptr;
}
}
// Access elements --------------------------------------------------------------------------------
T front() const { return head ? head->data : 0; };
T back() const { Node* n = getLast(); return n ? n->data : 0; }
// Add iterator properties to class ---------------------------------------------------------------
struct iterator { // Local class for iterator
Node* iter{}; // Iterator is basically a pointer to the node
// Define alias names necessary for the iterator functionality
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
// Constructor
iterator() {}
iterator(Node* n) : iter(n) {}
// Dereferencing
reference operator *() const { return iter->data; }
pointer operator ->() const { return &iter->data; }
// Aithmetic operations
iterator& operator ++() { if (iter) iter = iter->next; return *this; }
iterator operator ++(int) { iterator temp{ *this }; ++* this; return temp; }
iterator operator +(const difference_type& n) const { iterator temp{ *this }; difference_type k{ n }; while (k--)++temp; return temp; }
iterator& operator +=(const difference_type& n) { difference_type k{ n }; while (k--)++* this; return *this; };
// Comparison
bool operator != (const iterator& other) const { return iter != other.iter; }
bool operator == (const iterator& other) const { return iter == other.iter; }
bool operator < (const iterator& other) const { return iter < other.iter; }
bool operator > (const iterator& other) const { return iter > other.iter; }
bool operator <= (const iterator& other) const { return iter <= other.iter; }
bool operator >= (const iterator& other) const { return iter >= other.iter; }
// Difference. Also complicated, because no random access
difference_type operator-(const iterator& other) const {
difference_type result{};
Node* n{ iter };
while (n and n != other.iter) {
++result;
n = n->next;
}
return result;
}
};
// Begin and end function to initialize an iterator
iterator begin() const { return iterator(head); }
iterator end() const { return iterator(); }
// Functions typcical for forward lists ----------------------------------------------------------------------
// Easy, becuase we can operate form the current iterator and do not need the "previous" element
iterator insertAfter(iterator& pos, const T& i) {
iterator result{};
if (pos.iter and pos.iter->next) {
Node* n = new Node(i, pos.iter->next);
pos.iter->next = n;
result = n;
}
return result;
}
iterator eraseAfter(iterator& pos) {
iterator result{};
if (pos.iter and pos.iter->next) {
Node* tmp = pos.iter->next->next;
delete pos.iter->next;
pos.iter->next = tmp;
result = pos.iter->next;
}
return result;
}
};
// Test/Driver Code
int main() {
// Example for initilizer list
SinglyLinkedList<int> sllbase{ 5,6,7,8,9,10,11,12,13,14,15 };
// Show move constructor
SinglyLinkedList<int> sll(std::move(sllbase));
// Add some values in the front
sll.push_front(4);
sll.push_front(3);
sll.push_front(2);
sll.push_front(1);
// Delete 1st element (Number 1)
sll.pop_front();
// Delete last element
sll.pop_back();
// Use a std::algorithm on our custom linked list. Works because we have an interator
SinglyLinkedList<int>::iterator iter = std::find(sll.begin(), sll.end(), 8);
// Now add an element after 8
iter = sll.insertAfter(iter, 88);
// End delete the 9
iter = sll.eraseAfter(iter);
// Use range based for loop. Works because, we have iterators
for (int i : sll)
std::cout << i << ' ';
}
I've tried looking at other similar questions, but they all seem to contain answers regarding a feature of c11 I'm using.
When running the following code I get the error:
error: request for member 'current' in
'((mtm::MtmSet)this)->mtm::MtmSet::it.mtm::MtmSet::iterator::operator->()', which is of non-class type 'const int'
it->current = NULL;
in several parts of my MtmSet.h file.
These are the files I'm using. I'm truly at loss, so I fear I don't know which part is relevant, but have tried to omit as much as possible.
Error lines are marked Bold+Underline.
main.cpp:
#include "MtmSet.h"
int main() {
MtmSet<int> set;
return 0;
}
MtmSet.h:
#ifndef MTM4_SET_H
#define MTM4_SET_H
#include <cstdlib>
namespace mtm{
template<typename Type>
class MtmSet{
/**
* A node in the set
*/
class Node{
Type data;
Node* next;
public:
Type& getData() {
return data;
}
Node getNext() {
return *next;
}
Node() = default;
explicit Node(Type data) : data(data), next(NULL) {}
};
Node* first;
public:
//Forward declaration
class const_iterator;
/**
* A iterator for Set
*/
class iterator{
Node* current;
public:
/**
* Empty constructor. Should not be dereferenced.
* Same as MtmSet::end()
*/
iterator() = default;
/**
* Constructor of Set iterator
* #param node The node the iterator points to
*/
explicit iterator(Node *node){
current = node;
}
/**
* Copy constructor
* #param it The iterator to copy
*/
iterator(const iterator& it){
*this = it;
}
/**
* Destructor
*/
~iterator() = default;
iterator& operator=(const iterator& rhs){
current = rhs.current;
return *this;
}
const Type& operator*() const{
return current->getData();
}
const Type *operator->() const{
return current->getData();
}
iterator& operator++(){
*current = current->getNext();
return *this;
}
iterator operator++(int){
iterator it = *this;
*current = current->getNext();
return it;
}
bool operator==(const const_iterator& rhs) const{
if (current == rhs.current) {
return true;
}
return false;
}
bool operator!=(const const_iterator& rhs) const{
return !(current == rhs.current);
}
friend class const_iterator;
};
iterator it;
const_iterator const_it;
/**
* Empty constructor
* Creates an empty set
*/
MtmSet() : first(NULL){
First Error on it->current = NULL, and const_it->current = NULL below,
while all other lines return the same error:
it->current = NULL;
const_it->current = NULL;
}
/**
* Copy constructor
* #param set the Set to copy
*/
MtmSet(const MtmSet& set){}
/**
* Destructor
* Free all allocated memory in the set.
*/
~MtmSet() = default;
iterator insert(const Type& elem){
Node *a = new Node(elem);
if (first == NULL) {
first = a;
const_it->current = a;
it->current = a;
return it;
}
if (contains(elem)) {
return it;
}
while (it->current->next != NULL) {
it->current = it->current->next;
}
it->current->next = a;
it->current = it->current->next;
return it;
}
iterator find(const Type& elem){}
const_iterator find(const Type& elem) const{
it->current = first;
while (it->current != NULL) {
if (it->current->data == elem) {
return it;
}
}
return NULL;
}
bool contains(const Type& elem) const{
if (find(elem)) {
return true;
}
return false;
}
};
} // namespace mtm
#endif //MTM4_SET_H
Any help would be greatly appreciated.