I'm trying to construct an iterator for a given tree, however I'm getting
error: base operand of '->' has non-pointer type 'SearchTree::Iterator'
I believe its due to the way i've tried to construct x but I'm not entirely sure what the problem is there.
All the headers and cpp files are from the book so there's nothing there that's incorrect. Its just given what they've given me, I can't figure out a way to construct this iterator.
Main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "AVLTree.h"
#include "RandInt.h"
#include "SearchTree.h"
using std::cout;
using std::endl;
using std::string;
using std::to_string;
int main(int argc, const char* argv[]) {
// Set up random sequence generator
constexpr int maxv = 3249398;
RandInt rand {RandInt::getSeed(argc, argv), maxv};
AVLTree t{};
SearchTree st {};
AVLTree avl {};
// condition for duplicates
for (int i = 0; i < 5000 ; i++)
{
int random_value = rand();
string stringy = to_string(random_value);
if (t.find(k)==t.end())
{
t.insert (random_value, stringy);
}
else {}
}
SearchTree::Iterator x = SearchTree::Iterator(t.begin());
while (x != t.end()){
int k = x->key();
string str = x->value();
st.insert(k,str);
avl.insert(k,str);
++x;
}
return 0;
}
SearchTree.h
#ifndef __Binary_Search_Trees__SearchTree__
#define __Binary_Search_Trees__SearchTree__
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include "AVLEntry.h"
#include "BinaryTree.h"
#include "Entry.h"
#include "Exception.h"
using std::string;
using ET = AVLEntry;
class SearchTree {
public:
using K = ET::Key;
using V = ET::Value;
class Iterator;
protected:
using BinTree = BinaryTree<ET>;
using TPos = BinTree::Position;
protected:
BinTree T;
int n;
long comparisons;
public:
SearchTree();
SearchTree(const SearchTree& st) = delete;
SearchTree& operator=(const SearchTree& st) = delete;
int size() const { return n; }
bool empty() const { return T.root().left().isExternal(); }
Iterator find(const K& k);
Iterator insert(const K& k, const V& x);
void restructure(TPos& x);
void erase(const K& k);
void erase(const Iterator& p);
Iterator begin() const;
Iterator end() const;
long getComparisons() const { return comparisons; }
void clearComparisons() { comparisons = 0L; }
void debugPrintTree(std::ostream& os, const string& name) const;
protected:
TPos root() const;
std::pair<TPos, int> finder(const K& k, TPos v);
TPos inserter(const K& k, const V& x);
TPos eraser(TPos& v);
constexpr static int indent {4};
void debugPrintTree(std::ostream& os, const TPos& subRoot, int depth) const;
void printIndented(std::ostream& os, const char* st, int depth) const;
void printIndented(std::ostream& os, const ET& val, int depth) const;
public:
class Iterator {
private:
TPos v;
public:
Iterator(const TPos& vv) : v(vv) {}
const ET& operator*() const { return *v; }
ET& operator*() { return *v; }
TPos pos() const { return v; }
bool operator==(const Iterator& p) const { return v == p.v; }
bool operator!=(const Iterator& p) const { return ! (*this == p); }
Iterator& operator ++();
friend class SearchTree;
};
};
#endif /* defined(__Binary_Search_Trees__SearchTree__) */
entry.h
#ifndef Binary_Search_Trees_Entry_h
#define Binary_Search_Trees_Entry_h
#include <iostream>
template <typename K, typename V>
class Entry {
public:
using Key = K;
using Value = V;
public:
Entry(const K& k = K(), const V& v = V()) : _key(k), _value(v) {}
const K& key() const { return _key; }
const V& value() const { return _value; }
void setKey(const K& k) { _key = k; }
void setValue(const V& v) { _value = v; }
private:
K _key;
V _value;
};
template<typename K, typename V>
std::ostream& operator<<(std::ostream& os, const Entry<K, V>& e)
{
return os << '(' << e.key() << ", " << e.value() << ')';
}
#endif