1

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
TigerCode
  • 315
  • 3
  • 15
  • You may be using `->` instead of `.` – kiner_shah Dec 05 '16 at 10:45
  • 1
    Iterators aren't special, and they definitely aren't automatically handled like pointers by the compiler. That means you have to overload the `->` operator, just like you overloaded the dereference operator. – Some programmer dude Dec 05 '16 at 10:46
  • On an unrelated note, don't use symbols with leading underscores followed by another underscore or by an upper-case letter. [Those are reserved for the "implementation"](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Dec 05 '16 at 10:48
  • @kiner_shah I tried . and got `error:: class SearchTree::Iterator has no member named 'key'` – TigerCode Dec 05 '16 at 10:49
  • @Someprogrammerdude I'm not supposed to overload anything - just use whatever they've given me with the .h and .cpp files. - overloading -> would be a very nice solution however. – TigerCode Dec 05 '16 at 10:51
  • @TigerCode, I can see that those functions are present in class Entry of entry.h. – kiner_shah Dec 05 '16 at 10:53
  • Then you need to use what the `->` operator kind of means: Dereference and member access. For a pointer to a structure `p` and member `m` the expression `p->m` is equal to `(*p).m`. Now try to apply it to your iterator (which is supposed to emulate a pointer). – Some programmer dude Dec 05 '16 at 10:54
  • That worked, I see that the * was overloaded which allowed this to work instead of -> now. Thank you! – TigerCode Dec 05 '16 at 11:04

0 Answers0