Accessing a Private struct from child class - c++

I am required to implement these two methods in this class. Elem& operator*() and Elem* operator->(). The only issue whoever is that the Iterator class is defined within a Map Class. While the Elem is defined in the private section of the parent class. The catch is that I am not allowed to modify the the .h file of the class.
class Iterator{
public:
Iterator(){}
explicit Iterator(Elem *cur):_cur(cur) {}
Elem& operator*();
Elem* operator->();
// Iterator operator++(int);
bool operator==(Iterator it);
bool operator!=(Iterator it);
private:
Elem* _cur;
};
Here is my attempted implemnetation of the function. However does not work as it says the struct is private.
Map::Elem& Map::Iterator::operator*(Iterator it){
//do stuff
}
The class is defined within another class. Which the struct is defined in under the private section. I am not really sure how I am supposed to be returning an Elem& or Elem* from within the Iterator class, if the Elem structure is private. However I suspect it has something to do with the Elem* _cur; defined within the private function of the Iterator class.
Here is the struct defined within the Map class. If that makes sense.. its private...
private:
struct Elem {
KEY_TYPE key;
VALUE_TYPE data;
Elem *left;
Elem *right;
};
Elem *_root; // a dummy root sentinel
int _size;
In case what I included does not work, here is the full class definition. Just wanted to include the examples above to include less code.
#ifndef MAP_H
#define MAP_H
#include <iostream>
#include <string>
using namespace std;
typedef string KEY_TYPE;
typedef string VALUE_TYPE;
class Map{
struct Elem; //declaration of an interal structure needed below...
public:
//---Constructors and destructors---
Map(); // constructs empty Map
Map(const Map &rhs); // copy constructor
~Map(); // destructor
// assignment operator
Map& operator=(const Map &rhs);
// insert an element; return true if successful
bool insert(KEY_TYPE, VALUE_TYPE);
// remove an element; return true if successful
bool erase(KEY_TYPE);
// return size of the Map
int size() const;
// return an iterator pointing to the end if an element is not found,
// otherwise, return an iterator to the element
class Iterator;
Iterator find(KEY_TYPE) const;
// Iterators for accessing beginning and end of collection
Iterator begin() const;
Iterator end() const;
// overloaded subscript operator
VALUE_TYPE& operator[](KEY_TYPE);
// output the undering BST
ostream& dump(ostream& out) const;
// a simple Iterator, won't traverse the collection
class Iterator{
public:
Iterator(){}
explicit Iterator(Elem *cur):_cur(cur) {}
Elem& operator*();
Elem* operator->();
// Iterator operator++(int);
bool operator==(Iterator it);
bool operator!=(Iterator it);
private:
Elem* _cur;
};
private:
struct Elem {
KEY_TYPE key;
VALUE_TYPE data;
Elem *left;
Elem *right;
};
Elem *_root; // a dummy root sentinel
int _size;
// helper method for inserting record into tree.
bool insert(Elem *& root, const KEY_TYPE& key, const VALUE_TYPE& data);
// helper method for print tree
void printTree(ostream& out, int level, Elem *p) const;
// common code for deallocation
void destructCode(Elem *& p);
// common code for copy tree
void copyCode(Elem* &newRoot, Elem* origRoot);
};
ostream& operator<< (ostream&, const Map&);
#endif
Any help would be awesome. Been making the rounds on google with no such luck.

The issues is not that Elm is private. Change
Map::Elem& Map::Iterator::operator*(Iterator it){
//do stuff
}
to
Map::Elem& Map::Iterator::operator*(){
//do stuff
}
because the former does not match the signature declared in the header. That causes the defined operator overload to not be in the scope of the class.

Related

Can't access to Iterator from derived class

In summary, my problem is that I want to find a way to get an access to the element to which iterator is pointing from a derived class. The details are at the below.
I am implementing a sequence with a doubly linked list, referring to the book "Data Structures & Algorithms in C++" (written by Goodrich, Tamassia, Mount), page 255. The book defines class NodeSequence as a derived class of doubly linked list.
Here is the code. Firstly, I made a doubly linked list class with struct Node and class Iterator inside. (This is saved as "DList.h")
typedef int E;
class DList {
private:
struct Node {
E element;
Node* prev;
Node* next;
};
public:
class Iterator {
public:
E& operator*();//reference to the element
bool operator==(const Iterator& p) const;
bool operator!=(const Iterator& p) const;
Iterator& operator++();
Iterator& operator--();
friend class DList; //gives access to the DList
private:
Node* v; //pointer to the node
Iterator(Node* u); //create from Node
};
public:
DList();
int size() const;
bool empty() const;
Iterator begin() const;
Iterator end() const;
void insertFront(const E& e);
void insertBack(const E& e);
void insert(const Iterator& p, const E& e);
void eraseFront();
void eraseBack();
void erase(const Iterator& p);
void printList();
private:
int n;
Node* header;
Node* trailer;
};
Next, I made a derived class of DList named "NodeSequence", which is defined as the following
#include "DList.h"
class NodeSequence : public DList{ //NodeSequence class inherits DList class
public:
Iterator atIndex(int i) const ; //get position from index
int indexOf(const Iterator& p) const; //get index from position
};
Finally, I wanted to test my code, so I demonstrated as the following.
int main() {
NodeSequence test;
test.insertBack(5);
test.insertBack(4);
test.insertBack(3);
test.insertBack(2);
test.insertBack(1);
NodeSequence::Iterator p= test.atIndex(1);
cout << (p.v)->element() << endl;
// I expected 4, but it gives compile error, it says I can't access to p.v because it is a private member
}
Is there any way I can get an access to the private element (which is a pointer) of the iterator? I do not want to change Node* v from private to public.

Nested class - defining function that returns an object

I am trying to define this operator overload function. However, I am getting this error from the compiler:
List.hpp:62:1: error: invalid use of template-name ‘cop4530::List’ without an argument list
List& List<T>::operator=(List&& rhs) // move assingment operator
This is whole class that I am working on it. It is a class that implements a doubly linked list:
template <typename T>
class List {
private:
struct Node {
T data;
Node *prev;
Node *next;
Node(const T & d = T{}, Node *p = nullptr, Node *n = nullptr)
: data{d}, prev{p}, next{n} {}
Node(T && d, Node *p = nullptr, Node *n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};
public:
//nested const_iterator class
class const_iterator {
public:
const_iterator(); // default zero parameter constructor
const T & operator*() const; // operator*() to return element
// increment/decrement operators
const_iterator & operator++();
const_iterator operator++(int);
const_iterator & operator--();
const_iterator operator--(int);
// comparison operators
bool operator==(const const_iterator &rhs) const;
bool operator!=(const const_iterator &rhs) const;
protected:
Node *current; // pointer to node in List
T & retrieve() const; // retrieve the element refers to
const_iterator(Node *p); // protected constructor
friend class List<T>;
};
// nested iterator class
class iterator : public const_iterator {
public:
iterator();
T & operator*();
const T & operator*() const;
// increment/decrement operators
iterator & operator++();
iterator operator++(int);
iterator & operator--();
iterator operator--(int);
protected:
iterator(Node *p);
friend class List<T>;
};
public:
// constructor, desctructor, copy constructor
List(); // default zero parameter constructor
List(const List &rhs); // copy constructor
List(List && rhs); // move constructor
// num elements with value of val
explicit List(int num, const T& val = T{});
// constructs with elements [start, end)
List(const_iterator start, const_iterator end);
// constructs with a copy of each of the elements in the initalizer_list
List (std::initializer_list<T> iList);
~List(); // destructor
// copy assignment operator
const List& operator=(const List &rhs);
// move assignment operator
List & operator=(List && rhs);
// sets list to the elements of the initializer_list
List& operator= (std::initializer_list<T> iList);
// member functions
int size() const; // number of elements
bool empty() const; // check if list is empty
void clear(); // delete all elements
void reverse(); // reverse the order of the elements
T& front(); // reference to the first element
const T& front() const;
T& back(); // reference to the last element
const T& back() const;
void push_front(const T & val); // insert to the beginning
void push_front(T && val); // move version of insert
void push_back(const T & val); // insert to the end
void push_back(T && val); // move version of insert
void pop_front(); // delete first element
void pop_back(); // delete last element
void remove(const T &val); // remove all elements with value = val
template <typename PREDICATE>
void remove_if(PREDICATE pred); // remove all elements for which Predicate pred
// returns true. pred can take in a function object
// print out all elements. ofc is deliminitor
void print(std::ostream& os, char ofc = ' ') const;
iterator begin(); // iterator to first element
const_iterator begin() const;
iterator end(); // end marker iterator
const_iterator end() const;
iterator insert(iterator itr, const T& val); // insert val ahead of itr
iterator insert(iterator itr, T && val); // move version of insert
iterator erase(iterator itr); // erase one element
iterator erase(iterator start, iterator end); // erase [start, end)
private:
int theSize; // number of elements
Node *head; // head node
Node *tail; // tail node
void init(); // initialization
};
The function that I am trying to define is this one:
template < typename T>
List& List<T>::operator=(List&& rhs)
{
std::swap( theSize, rhs.theSize);
std::swap( head, rhs.head);
std::swap( tail, rhs.tail);
return *this;
}
You forgot to add <T>:
template <typenameT>
List<T>& List<T>::operator=(List<T>&& rhs)
// ^^^ ^^^
// ^ this one is optional but I prefer it
{
//...
}

checking which object an iterator is a position in

I have the following List class:
typedef int Elem; // list base element type
class NodeList { // node-based list
private:
struct Node { // a node of the list
Elem elem; // element value
Node* prev; // previous in list
Node* next; // next in list
};
public:
class Iterator { // an iterator for the list
public:
Elem& operator*(); // reference to the element
bool operator==(const Iterator& p) const; // compare positions
bool operator!=(const Iterator& p) const;
Iterator& operator++(); // move to next position
Iterator& operator--(); // move to previous position
friend class NodeList; // give NodeList access
private:
Node* v; // pointer to the node
Iterator(Node* u); // create from node
};
public:
NodeList(); // default constructor
int size() const; // list size
bool empty() const; // is the list empty?
Iterator begin() const; // beginning position
Iterator end() const; // (just beyond) last position
void insertFront(const Elem& e); // insert at front
void insertBack(const Elem& e); // insert at rear
void insert(const Iterator& p, const Elem& e); // insert e before p
void eraseFront(); // remove first
void eraseBack(); // remove last
void erase(const Iterator& p); // remove p
private: // data members
int n; // number of items
Node* header; // head-of-list sentinel
Node* trailer; // tail-of-list sentinel
};
My code does not do any checking to determine whether a given position (iterator object) is actually a member of a particular list. For example, if p is a position in list S and I call T.insert(p,e) on a different list T, then I'd actually be adding the element to S just before p. How can I change my NodeList implementation to disallow such a misuse?
It would mean a bit of memory overhead but if you store the head of each list in all nodes you can check if the heads are the same, then it's probably the same list.
Or if you prefer cpu overhead over memory overhead, loop through the prev-links to find the head of both lists and compare them as above.
So it depends on which kind of overhead you prefer.

defining iterators for a generic tree

I have this class called "Node". I've been considering renaming it "Tree", but either name makes about as much sense. This class implements a generic tree container. Each node can have any number of children. The basic header definition of the class is as follows:
template<class Elem>
class Node
{
public:
Node();
~Node();
Node(const Elem& value);
Node(const Node& rNode);
const Elem& operator*() const;
Elem& operator*();
Elem* operator->();
void operator=(const Elem& rhs);
Node* addChild(const Elem& value);
Node* addChild(Node childNode);
Node* addChild(Node* pChildNode);
HRESULT removeNode(DFSIterator<Node>& iter);
template <class Node, class List, class Iter> friend class DFSIterator;
private:
bool hasChild() const;
Node* m_pParentNode;
Elem m_value;
std::vector<Node*> m_childList;
static std::set<Node*> sNodeSet;
};
The header definition of my DFSIterator is:
template<class Item,
class List = std::vector<Item*>,
class Iter = typename std::vector<Item*>::iterator>
class DFSIterator
{
public:
DFSIterator(Item& rRootNode);
~DFSIterator();
DFSIterator* begin();
DFSIterator* operator++();
Item& operator*() const;
Item* operator->() const;
bool operator!=(const DFSIterator& rhs) const;
bool isDone() const;
operator bool() const {return !isDone();}
private:
template <class Node> friend class Node;
void initChildListIterator(Item* currentNode);
bool m_bIsDone;
Item* m_pRootNode;
Item* m_pCurrentNode;
ChildListIterator<Item>* m_pCurrentListIter;
std::map<Item*, ChildListIterator<Item, List, Iter>*> m_listMap;
};
Item is the iterator's alias for Node<Elem>.
The problem I am having is that I want to define iterators for this tree that the user can declare in a similar way to STL containers. I was thinking that putting typedef statements like typedef DFSIterator<Node<Elem>> dfs_iterator; would work fine. But whenever I add those statements into the header, I get the following error error C2512<Item>: no appropriate default constructor available. Wherever I try to go and use it.
So right now, to declare an iterator I have to do something like DFSIterator<Node<DataMap>> dfsIter = rRootNode.begin(); or DFSIterator<Node<DataMap>> dfsIter(rNode); if I don't want to start at the root node of the tree. What I want to be able to do is something more like Node<DataMap>::dfs_iterator it = rRootNode.begin(). Is there a way to do this that I am missing?
Note: I do want to change a few other things about this implementation. I don't really want the user to be passing a node element to the addChild() method. I'd rather have the user pass an iterator that is pointing to a node.
If you define dfs_iterator inside Node, then you can use it basically like you describe:
template<class Elem>
class Node
{
public:
typedef Node<Elem> Item;
template<
class List = std::vector<Item*>,
class Iter = typename std::vector<Item*>::iterator
> class dfs_iterator;
.
.
.
};
template<class Elem>
template<class List, class Iter>
class Node<Elem>::dfs_iterator
{
public:
.
.
.
};
and use
Node<DataMap>::dfs_iterator<> it = rRootNode.begin();
The only difference is that since dfs_iterator is a template, you have to specify the template parameters, even though they both can be defaulted.

struct reference and operator=

I have a class like this:
template <class T>
class bag
{
private:
typedef struct{T item; unsigned int count;} body;
typedef struct _node{_node* prev; body _body; _node* next;}* node;
struct iterator{
enum exception{NOTDEFINED, OUTOFLIST};
body operator*();
explicit iterator();
explicit iterator(const iterator&);
iterator& operator=(const iterator&);
iterator& operator++(int);
iterator& operator--(int);
bool operator==(const iterator&) const;
bool operator!() const;
private:
node current;
friend class bag;
};
node head;
node foot;
iterator _begin;
iterator _end;
/* ... */
public: /* ... */
bag();
const iterator& begin;
const iterator& end;
};
In the bag() I have to set the reference begin to _begin, and end to _end.
begin = _begin;
end = _end;
But I think this line
begin = _begin;
invokes bag::iterator::operator=() function.
How can i avoid that?
References can't be assigned, only initialised. So you will need to initialise them in the constructor's initialisation list:
bag() : begin(_begin), end(_end) {}
However, it's more conventional (and also reduces the class size) to get these using accessor functions rather than public references:
const iterator& begin() {return _begin;}
const iterator& end() {return _end;}
Use initializer list:
bag::bag() : begin(begin_), end(end_)
{
}
As Mike said, you have to initialise a reference where you declare it, you can't change it afterwards. The initialiser list is the most idiomatic solution if you don't have to modify the references once the object is constructed, but in the case you foresee you have to change them, there are still the good old pointers:
public: /* ... */
bag();
iterator *begin;
iterator *end;
A class containing reference members essentially becomes unassignable (because references can't be reseated).
It looks like your Bag is duplicating the same data in several members (extra burden to keep duplicated values in synch).
Instead you could do what the standard library does: create and return the iterator by value from begin() and end() methods.
template <class T>
class bag
{
struct node { /*...*/ }
/* ... */
node* head; //why would you want to typedef away that these are pointers?
node* tail;
public:
class iterator {
iterator(node*);
/* ... */
};
iterator begin() { return iterator(head); }
iterator end() { return iterator(tail); }
//also you might need const_iterators
};