Adding an Instance to a Linked List using a Node - c++

I'm having trouble understanding on how would I call the add function in main if I want to add an instance to list.
#include "object.h"
class list {
private:
struct Node
{
object objectInfo;
Node *next;
};
int size;
Node *head;
public:
list();
list(const list& otherlist);
~list();
void Add(const Node myObject);
my Main
int main() {
object myObject;
myObject.setTitle("Object 1");
myObject.setPrice(78.58);
myObject.setISBN("515161611");
cout << myObject << endl;
list myList;
myList.Add(myObject);
return 0;
}
my Function in cpp
void list::Add(const Node myObject) {
Node* temp;
temp = new Node;
temp->objectInfo = myObject.objectInfo;
temp->next = head;
head = temp;
size++;
}
Im having trouble on this line myList.Add(myObject);
keeps saying void list::Add(const list&)':cannot convert argument 1 from 'object' to 'const list::Node'
also no instance of overloaded function "list::Add" matches the argument list

You are trying to pass an object of type object into a function that takes a parameter of type Node. const Node myObject should be const object myObject.

This might give you some ideas:
#include <iostream>
#include <string>
template <typename Object>
class List {
class Node : public Object {
//static int& count() { static int c=0; return c; }
public:
Node* next;
Node() : next(nullptr) {
//std::cout << ++count() << " Nodes\n";
}
Node(const Object& obj) : next(nullptr), Object(obj) {
//std::cout << ++count() << " Nodes\n";
}
~Node() {
//std::cout << --count() << " Nodes\n";
}
};
Node *head, *tail;
int size;
public:
class iterator {
Node *cur_node;
public:
iterator(Node* node) { cur_node = node; }
Object& operator * () const { return *cur_node; }
bool operator != (const iterator& iter) const { return iter.cur_node != cur_node; }
iterator& operator ++() { if(cur_node) cur_node = cur_node->next; return *this; }
};
class iterator_const {
const Node *cur_node;
public:
iterator_const(const Node* node) { cur_node = node; }
const Object& operator * () const { return *cur_node; }
bool operator != (const iterator_const& iter) const { return iter.cur_node != cur_node; }
iterator_const& operator ++() { if(cur_node) cur_node = cur_node->next; return *this; }
};
iterator begin() { return iterator(head); }
iterator end() { return iterator(nullptr); }
iterator_const begin() const { return iterator_const(head); }
iterator_const end() const { return iterator_const(nullptr); }
template <typename ...Args>
void Add(const Object& obj, Args...more) {
Node *new_node = new Node(obj);
++size;
if(!tail) { tail = head = new_node; }
else { tail->next = new_node; tail = new_node; }
Add(more...);
}
void Add() {}
int Size() const { return size; }
List() : head(nullptr), tail(nullptr), size(0) {}
List(const List& src) : head(nullptr), tail(nullptr), size(0) {
for(auto&& entry : src) {
Add(entry);
}
}
~List() {
Node* p = head;
while(p) {
Node* next = p->next;
delete p;
p = next;
}
}
};
struct MyObjectType {
std::string name;
int age;
MyObjectType(std::string name, int age) : name(name), age(age) {}
friend std::ostream& operator << (std::ostream& os, const MyObjectType& obj) {
return os << "{\"" << obj.name << "\":" << obj.age << "}";
}
};
template <typename T>
void PrintList(const List<T>& list) {
std::cout << "Size: " << list.Size() << "\n";
for(const auto &elem : list) {
std::cout << " " << elem << "\n";
}
}
int main() {
using MyList = List<MyObjectType>;
MyList list_one;
list_one.Add(
MyObjectType("Harry",32),
MyObjectType("Lisa", 66),
MyObjectType("Buddy", 2),
MyObjectType("Skippy", 21)
);
MyList list_two(list_one);
list_two.Add(
MyObjectType("Horse", 10),
MyObjectType("Mule", 11)
);
std::cout << "list_one:\n";
PrintList(list_one);
std::cout << '\n';
std::cout << "list_two:\n";
PrintList(list_two);
}

Related

error: new initializer expression list treated as compound expression [-fpermissive]

I am trying to write a playlist method for songs in c++, however, I keep running into frequent errors.
template <typename T>
struct cir_list_node
{
T* data;
cir_list_node *next, *prev;
//destructor
~cir_list_node() {
delete data;
}
};
template <typename T>
struct cir_list
{
public:
using node = cir_list_node<T>; // renaiming for local use
using node_ptr = node*;
private:
node_ptr head;
size_t n;
public:
// basic constructor and size function
cir_list(): n(0) // initiator list
{
head = new node(NULL,NULL,NULL); //dummy node
head->next = head; // circular list wraps around
head->prev = head;
}
cir_list(const cir_list& other): cir_list()
{
// insert in reverse order (okay because list is circular)
for(const auto& i : other)
insert(i);
}
cir_list(const std::initializer_list<T>& il): head(NULL), n(0)
{
//insert in reverse order
for(const auto& i : il)
insert(i);
}
~cir_list()
{
while(size())
{
erase(head->data);
}
}
size_t size() const
{
return n;
}
void insert(const T& value)
{
node_ptr newNode = new node(new T(value),NULL,NULL);
n++;
auto dummy = head->prev;
dummy->next = newNode;
newNode->prev = dummy;
if(head == dummy) {
dummy->prev = newNode;
newNode->next = dummy;
head = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
return;
}
void erase(const T& value)
{
auto cur = head, dummy = head->prev;
while (cur != dummy){
if(*(cur->data) == value){
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
if(cur == head)
head = head->next;
delete cur;
n--;
return;
}
cur = cur->next;
}
}
struct cir_list_it
{
private:
node_ptr ptr;
public:
cir_list_it(node_ptr p) : ptr(p)
{}
T& operator*()
{
return *(ptr->data);
}
node_ptr get()
{
return ptr;
}
cir_list_it& operator++() // prefix
{
ptr = ptr->next;
return *this;
}
cir_list_it operator++(int) // postfix
{
cir_list_it it = *this;
++(*this);
}
cir_list_it& operator--() //prefix
{
ptr = ptr->prev;
return *this;
}
cir_list_it operator--(int) // postfix
{
cir_list_it it = *this;
--(*this);
return it;
}
friend bool operator == (const cir_list_it& it1, const cir_list_it& it2)
{
return it1.ptr == it2.ptr;
}
friend bool operator != (const cir_list_it& it1, const cir_list_it& it2)
{
return it1.ptr != it2.ptr;
}
};
cir_list_it begin()
{
return cir_list_it {head};
}
cir_list_it end()
{
return cir_list_it {head->prev};
}
cir_list_it end() const
{
return cir_list_it{head->prev};
}
};
struct playlist
{
cir_list<int> list;
void insert(int song)
{
list.insert(song);
}
void erase(int song)
{
list.erase(song);
}
void loopOnce()
{
for(auto& song : list){
std::cout << song << " ";
}
std::cout << std::endl;
}
};
int main()
{
playlist pl;
pl.insert(1);
pl.insert(2);
std::cout << "Playlist: ";
pl.loopOnce();
playlist pl2 = pl;
pl2.erase(2);
pl2.insert(3);
std::cout << "Second playlist";
pl2.loopOnce();
}
Errors
1 and 2:
3 and 4:
It seems there is a typo
struct cir_list_node
{
T* data;
cir_list_node *next, *prev;
//destructor
~cir_list_node() {
delete data;
}
};
You forgot to prefix this declaration with
template <typename T>
This template structure declaration declares an aggregate.
You can not initialized it with an expression inside parentheses like
head = new node(NULL,NULL,NULL);
Instead you need to write using braces
head = new node { NULL, NULL, NULL };
or as you are writing a C++ program then
head = new node { nullptr, nullptr, nullptr };
It seems there is a typo
struct cir_list_node
{
T* data;
cir_list_node *next, *prev;
//destructor
~cir_list_node() {
delete data;
}
};

How to implement iterator design pattern on C++?

I'm curious about how to implement the iterator pattern the way STL does in a stack ADT.
#include <iostream>
#include <vector>
int main() {
std::vector<char> v = { 'a', 'b', 'c', 'd', 'e', 'f'};
std::vector<char>::iterator it = v.begin();
while(it != v.end()) {
std::cout << *it << "->";
++it;
}
std::cout << "\n";
return 0;
}
Output
a->b->c->d->e->f->
so far I have implemented the following code
#include <iostream>
#include <memory>
template<class T>class Node {
private:
T data = 0;
std::shared_ptr<Node<T>> next_node = nullptr;
public:
Node(T data = 0, std::shared_ptr<Node<T>> next_node = nullptr)
: data(data), next_node(next_node)
{
std::cout << "created node[" << data << "]\n";
}
~Node() {
std::cout << "deleted node[" << data << "]\n";
}
// getters and setters
T getData() const {
return this->data;
}
std::shared_ptr<Node<T>> getNextNode() const {
return this->next_node;
}
void setData(T value) {
this->data = value;
}
void setNextNode(std::shared_ptr<Node<T>> node) {
this->next_node = node;
}
};
template<class T>std::ostream& operator<<(std::ostream& o, const std::shared_ptr<Node<T>> node) {
return o << "node["<< node->getData() <<"]-> ";
}
template<class T>class Stack {
private:
std::shared_ptr<Node<T>> top = nullptr;
public:
Stack()
: top(nullptr)
{ /* empty */ }
~Stack() { /* empty */ }
void push(T value) {
if(!top) {
top = std::shared_ptr<Node<T>> (new Node<T>(value));
} else {
top = std::shared_ptr<Node<T>> (new Node<T>(value, top));
}
}
void display() {
if(!top) {
std::cout << "display::The stack is empty.\n";
} else {
std::shared_ptr<Node<T>> p = top;
while(p) {
std::cout << p;
p = p->getNextNode();
}
std::cout << "\n";
}
}
class Iterator {
private:
std::shared_ptr<Node<T>> node;
public:
Iterator(std::shared_ptr<Node<T>> node)
: node(node)
{ /* empty */ }
bool hasMore() {
return node->getNextNode() != nullptr;
}
Iterator getNext() {
return Iterator(node->getNextNode());
}
int getData() {
return node->getData();
}
};
Iterator begin() const {
return Iterator(top);
}
Iterator getIterator() {
Iterator it = Iterator(top);
return it;
}
};
int main() {
Stack<char> stack;
for(char i = 'a'; i < 'f'; ++i) {
stack.push(i);
}
Stack<char>::Iterator it = stack.begin();
while(it.hasMore()) {
std::cout << it.getData() << "->";
it = it.getNext();
}
std::cout << "\n";
return 0;
}
Output:
created node[a]
created node[b]
created node[c]
created node[d]
created node[e]
101->100->99->98->
deleted node[e]
deleted node[d]
deleted node[c]
deleted node[b]
deleted node[a]
My question is how to implement nested template detection for the Iterator class, as you can see the expected output is a char type and I am getting integers.
Can someone help me understand how this is implemented in the STL and how it could be implemented in an ADT?
thanks!!!
Thanks for the comments I be able to fix the problem I was returning the wrong data type on int getData() { return node->getData(); } I just change the int type for T type and everithing works ok!
also change the hasMore method for bool hasMore() { return node != nullptr; }
#include <iostream>
#include <memory>
template<class T>class Node {
private:
T data = 0;
std::shared_ptr<Node<T>> next_node = nullptr;
public:
Node(T data = 0, std::shared_ptr<Node<T>> next_node = nullptr)
: data(data), next_node(next_node)
{
std::cout << "created node[" << data << "]\n";
}
~Node() {
std::cout << "deleted node[" << data << "]\n";
}
// getters and setters
T getData() const {
return this->data;
}
std::shared_ptr<Node<T>> getNextNode() const {
return this->next_node;
}
void setData(T value) {
this->data = value;
}
void setNextNode(std::shared_ptr<Node<T>> node) {
this->next_node = node;
}
};
template<class T>std::ostream& operator<<(std::ostream& o, const std::shared_ptr<Node<T>> node) {
return o << "node["<< node->getData() <<"]-> ";
}
template<class T>class Stack {
private:
std::shared_ptr<Node<T>> top = nullptr;
public:
Stack()
: top(nullptr)
{ /* empty */ }
~Stack() { /* empty */ }
void push(T value) {
if(!top) {
top = std::shared_ptr<Node<T>> (new Node<T>(value));
} else {
top = std::shared_ptr<Node<T>> (new Node<T>(value, top));
}
}
void display() {
if(!top) {
std::cout << "display::The stack is empty.\n";
} else {
std::shared_ptr<Node<T>> p = top;
while(p) {
std::cout << p;
p = p->getNextNode();
}
std::cout << "\n";
}
}
class Iterator {
private:
std::shared_ptr<Node<T>> node;
public:
Iterator(std::shared_ptr<Node<T>> node)
: node(node)
{ /* empty */ }
bool hasMore() {
return node != nullptr;
}
Iterator getNext() {
return Iterator(node->getNextNode());
}
T getData() {
return node->getData();
}
};
Iterator begin() const {
return Iterator(top);
}
Iterator getIterator() {
Iterator it = Iterator(top);
return it;
}
};
int main() {
Stack<char> stack;
for(char i = 'a'; i < 'f'; ++i) {
stack.push(i);
}
Stack<char>::Iterator it = stack.begin();
while(it.hasMore()) {
std::cout << it.getData() << "->";
it = it.getNext();
}
std::cout << "\n";
return 0;
}
Output
created node[a]
created node[b]
created node[c]
created node[d]
created node[e]
e->d->c->b->a->
deleted node[e]
deleted node[d]
deleted node[c]
deleted node[b]
deleted node[a]

Polymorphic function for Print & reverse Print in C++ within a single class

I have written two methods for printing my linked list data. One is print which works normally. And one is for printing the list in reverse order. I want to make my print method to accept a Node* ptr and do the rest of the work as usual. I'm not getting the idea how to make it work because of the operator overloading. My methods are as follows. Please ignore the void* casting and return in the reversePrint method.
template <class T>
std::ostream& operator <<(std::ostream& str, LinkedList<T> const& data){
data.print(str);
return str;
}
template <class T>
void LinkedList<T>::print(std::ostream& str) const{
Node *curr = head;
while(curr != nullptr){
str << curr->data << "\n";
curr = curr->next;
}
}
template <class T>
void* LinkedList<T>::printReverse(std::ostream& str) {
Node* curr = head;
//Node* last = (Node*)getLastNode(head);
Node* rHead = nullptr;
while(curr != nullptr){
Node* temp = curr->next;
curr->next = rHead;
rHead = curr;
curr = temp;
}
while(rHead != nullptr){
std::cout << rHead->data << "\n";
rHead = rHead->next;
}
return (void*)rHead;
}
class definition.
template <class T>
class LinkedList {
private:
struct Node {
T data;
Node *next;
Node(T data, Node *next) :
data(data), next(next) {
}
};
Node* head;
public:
LinkedList() :
head(nullptr) {
}
~LinkedList() {
Node *temp;
for (; head; head = temp) {
temp = head->next;
std::cout << "Destructor called for " << temp->data;
delete head;
}
}
void append(T item);
void insert_at_head(T value);
void* getLastNode(Node* n);
void print(std::ostream& str = std::cout) const;
void* printReverse(std::ostream& str = std::cout);
};
printReverse probably should not modify the list.
I would write it recursively. Idea:
template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
if (!head) return;
printReverse(str, head->next);
std::cout << head->data << "\n";
}
With e.g. declarations in-class:
void printReverse(std::ostream &str) const { printReverse(str, head); }
private:
void static printReverse(std::ostream &str, Node const* head);
Off-topic:
consider const-correctness
consider Command/Query Separation (separate node iteration from printing)
Live On Coliru
#include <iostream>
template <class T> class LinkedList {
private:
struct Node {
T data;
Node *next;
//Node(T data, Node *next) : data(data), next(next) {}
};
Node *head;
public:
LinkedList() : head(nullptr) {}
~LinkedList() {
while (head) {
Node* temp = head->next;
std::cout << "Destructor called for " << head->data;
delete head;
head = temp;
}
}
void append(T item) {
Node* t = getLastNode(head);
(t?t->next : head) = new Node { item, nullptr };
}
void insert_at_head(T value);
Node const*getLastNode(Node const*n) const {
for (Node const* it = n; it; it = it->next)
if (!it->next) return it;
return nullptr;
}
Node*getLastNode(Node*n) {
for (Node* it = n; it; it = it->next)
if (!it->next) return it;
return nullptr;
}
void print(std::ostream &str = std::cout) const;
void printReverse(std::ostream &str) const { printReverse(str, head); }
private:
void static printReverse(std::ostream &str, Node const* head);
};
template <class T> std::ostream &operator<<(std::ostream &str, LinkedList<T> const &data) {
data.print(str);
return str;
}
template <class T> void LinkedList<T>::print(std::ostream &str) const {
for (Node const* curr=head; curr; curr = curr->next) {
str << curr->data << "\n";
}
}
template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
if (!head) return;
printReverse(str, head->next);
std::cout << head->data << "\n";
}
int main() {
LinkedList<int> ll;
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.print(std::cout);
ll.printReverse(std::cout);
}
Prints
1
2
3
4
4
3
2
1

c++ how implement iterator for a doubly linked list

I am using this text book
http://cpp.datastructures.net
chapter 5 iterator usage:
http://cpp.datastructures.net/source/ch05/CPP/IteratorPrint.cpp-DSACiterator.html
here is how i implement it (the ObjectIterator class)
#include <iostream>
using namespace std;
class PositionException {
protected:
string message;
public:
PositionException(const string &m) {
message = m;
}
void print() {
cout << message << endl;
}
};
template <typename T>
class NodeList {
protected:
struct Node {
Node *prev;
Node *next;
T item;
Node(T i = T(), Node *p = NULL, Node *n = NULL) : item(i), prev(p), next(n) {}
};
typedef Node * NodePtr;
public:
class Position {
protected:
NodePtr node;
public:
bool isNull() {
return node == NULL;
}
Position(NodePtr n = NULL) : node(n) {}
T& element() {
return node->item;
}
friend class NodeList;
};
class ObjectIterator {
protected:
NodePtr node;
public:
ObjectIterator(NodePtr n = NULL) : node(n) {}
bool hasNext() {
if (node->next == NULL) {
return false;
}
if (node->next->next == NULL) {
return false;
}
return true;
}
ObjectIterator next() {
return ObjectIterator(node->next);
}
T& element() {
return node->item;
}
friend class NodeList;
};
protected:
int sz;
NodePtr header;
NodePtr trailer;
public:
NodeList() {
header = new Node();
trailer = new Node();
header->next = trailer;
trailer->prev = header;
sz = 0;
}
bool isEmpty() const {
return size() == 0;
}
int size() const {
return sz;
}
bool isFirst(const Position& p) const {
return p.node->prev == header;
}
bool isLast(const Position& p) const {
return p.node->next = trailer;
}
Position first() const {
if (isEmpty()) {
throw PositionException("no first for empty list");
}
return Position(header->next);
}
Position last() const {
if (isEmpty()) {
throw PositionException("no last for emtpy list");
}
return Position(trailer->prev);
}
Position before(const Position& p) const{
if (p.node->prev == header) {
throw PositionException("already the first element, nothing before it");
}
return Position(p.node->prev);
}
Position after(const Position& p) const{
if (p.node->next == trailer) {
throw PositionException("already the last element, nothing after it");
}
return Position(p.node->next);
}
Position insertAfter(const Position& p, const T& o) {
NodePtr node = new Node(o, p.node, p.node->next);
p.node->next->prev = node;
p.node->next = node;
sz++;
return Position(node);
}
Position insertBefore(const Position& p, const T& o) {
NodePtr node = new Node(o, p.node->prev, p.node);
p.node->prev->next = node;
p.node->prev = node;
sz++;
return Position(node);
}
void remove(const Position& p) {
p.node->prev->next = p.node->next;
p.node->next->prev = p.node->prev;
sz--;
delete p.node;
}
void removeFirst() {
remove(first());
}
void removeLast() {
remove(last());
}
void replaceElement(const Position& p, const T& element) {
if (p.isNull()) {
throw PositionException("p is null");
}
p.node->item = element;
}
Position insertFirst(const T& o) {
NodePtr node = new Node(o, header, header->next);
header->next->prev = node;
header->next = node;
sz++;
return Position(node);
}
Position insertLast(const T& o) {
NodePtr node = new Node(o, trailer->prev, trailer);
trailer->prev->next = node;
trailer->prev = node;
sz++;
return Position(node);
}
void copyFrom(const NodeList<T>& nl) {
sz = nl.sz;
if (nl.sz > 0) {
Position p0 = nl.first();
Position p = insertFirst(p0.node->item);
while (!nl.isLast(p0)) {
p0 = nl.after(p0);
insertAfter(p, p0.node->item);
}
}
}
void emptyList() {
while (!isEmpty()) {
removeFirst();
}
}
~NodeList() {
emptyList();
delete header;
delete trailer;
}
NodeList<T>& operator=(const NodeList<T>& nl) {
emptyList();
copyFrom(nl);
}
NodeList(const NodeList<T>& nl) {
emptyList();
copyFrom(nl);
}
void print() {
cout << "size is: " << size() << endl;
if (size() > 0) {
ObjectIterator i = elements();
while (i.hasNext()) {
cout << i.element() << "\t";
i = i.next();
}
cout << endl;
}
}
ObjectIterator elements() {
if (isEmpty()) {
throw PositionException("iterator error: empty");
}
return ObjectIterator(header->next);
}
void swapItems(const Position& p1, const Position& p2) {
T temp = p1.node->item;
p1.node->item = p2.node->item;
p2.node->item = temp;
}
};
If i don't use the iterator, the following code for printing is correct
void print() {
if (size() > 0) {
NodePtr n = header->next;
while (n != trailer) {
cout << n->item << "\t";
n = n->next;
}
cout << endl;
}
}
If I use the iterator for the print function
void print() {
cout << "size is: " << size() << endl;
if (size() > 0) {
ObjectIterator i = elements();
while (i.hasNext()) {
cout << i.element() << "\t";
i = i.next();
}
cout << endl;
}
}
one of the node is missing.
The book does not provide the correct way to implement the iterator

Postincrementation operator in linked list

I had to write a program that handle this main code:(not allowed to change it)
list<int> iv;
iv["john"] = 23;
int ia = iv["john"]++;
int ib = iv["john"];
cout << ia << " " << ib << endl; // prints 23 24
try{
cout << iv["jack"] << endl; // should throw an exception
}catch(list<int>::Uninitialized&)
{
cout << "Uninitialized map element!" << endl;
};
Here is my code:
#ifndef EXAM_H
#define EXAM_H
#include <iostream>
#include <string>
using namespace std;
template <class TYPE>
class list
{
private:
struct node
{
TYPE value;
string index;
bool isInit;
node *next;
};
node *head;
node *current;
public:
class Cref
{
friend class list;
list& s;
string position;
Cref (list& ss, string pos): s(ss), position(pos) {};
public:
operator TYPE() const
{
return s.read(position);
}
Cref& operator = (TYPE val)
{
s.write(position,val);
return *this;
};
Cref& operator = (const Cref& ref)
{
return operator= ((TYPE)ref);
};
};
class Uninitialized{};
list ()
{
cout << "constructor\n";
head = NULL;
current = NULL;
}
~list ()
{
while (head)
{
node *t = head->next;
delete head;
head = t;
};
}
TYPE read (string ind) const
{
cout << "read\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) return t->value;
else t = t->next;
}
throw Uninitialized();
}
void write (string ind, TYPE value_)
{
cout << "write\n";
node *t = new node;
t->next = head;
head = t;
head->value = value_;
head->index = ind;
head->isInit = true;
}
TYPE operator[] (string ind) const
{
cout << "read\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) return t->value;
else t = t->next;
}
throw Uninitialized();
}
Cref operator[] (string ind)
{
return Cref(*this, ind);
}
};
#endif
Everything works great, but only when I comment out postincrementation operation in main program
int ia = iv["john"]++;
As you can see I have a struct node where I put all variables and I want to increment value by one in node where the key is "john". Is there any way to implement operator++ for this code ?
I am not allowed to use std::map.
The usual approach to your problem is defining the array subscript operators as
const TYPE& operator[](string ind) const;
TYPE& operator[](string ind);
In this way, you do not have to bother a single bit about the operator++: Since iv["John"] returns a reference to int, iv["John"]++ will call the int post-increment operator which is built-in.
Yes, I have already tried this solution, but compiler do not distinguish between reading and writing and still using non-const version. So I had to build proxy class Cref that helps to distinguish.
I have also already find a solution to operator++ problem.
This operation had to be from Cref level. I created
Cref& operator++ (int val)
{
s.increment(position,val);
return *this;
};
And increment function in main class body as follows:
void increment (string ind, int value_)
{
cout << "increment\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) t->value = t->value + 1;
t = t->next;
}
}
That fully solved my problem.