Joe Caraccio Joe Caraccio - 1 year ago 101
C++ Question

Accessing a Private struct from child class

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download