Ich habe die folgenden Klassen und ich versuche, den Operator * von dem inneren Klasse Iterator zu überlastenZugriff
#ifndef __LISTMAP_H__
#define __LISTMAP_H__
#include "xless.h"
#include "xpair.h"
template <typename Key, typename Value, class Less=xless<Key>>
class listmap {
public:
using key_type = Key;
using mapped_type = Value;
using value_type = xpair<const key_type, mapped_type>;
private:
Less less;
struct node;
struct link {
node* next{};
node* prev{};
link (node* next, node* prev): next(next), prev(prev){}
};
struct node: link {
value_type value{};
node (node* next, node* prev, const value_type&);
};
node* anchor() { return static_cast<node*> (&anchor_); }
link anchor_ {anchor(), anchor()};
public:
class iterator;
listmap(){};
listmap (const listmap&) = default;
listmap& operator= (const listmap&) = default;
~listmap();
iterator insert (const value_type&);
iterator find (const key_type&);
iterator erase (iterator position);
iterator begin() { return anchor()->next; }
iterator end() { return anchor(); }
bool empty() const { return begin() == end(); }
};
template <typename Key, typename Value, class Less>
class listmap<Key,Value,Less>::iterator {
private:
friend class listmap<Key,Value>;
listmap<Key,Value,Less>::node* where {nullptr};
iterator (node* where): where(where){};
public:
iterator(){}
value_type& operator*();
value_type* operator->();
iterator& operator++(); //++itor
iterator& operator--(); //--itor
void erase();
bool operator== (const iterator&) const;
bool operator!= (const iterator&) const;
};
template <typename Key, typename Value, class Less>
value_type& listmap<Key,Value,Less>::iterator<Key,Value,Less>::operator*()
{
return where->value;
}
#include "listmap.tcc"
#endif
Das Problem ist, dass value_type ist ein öffentliche aus der Klasse listmap und es ist nicht statisch, also weiß ich nicht, wie man die Erklärung des Operators *() vervollständigt. Ich möchte den Fehler nicht beheben, indem ich die Struktur des Codes ändere. Ex: Herstellung
using value_type = xpair<const key_type, mapped_type>;
Global. Ich frage mich nur, ob es einen anderen Trick gibt, den ich verwenden kann, um value_type aufzurufen.
.... edit: Ich habe keine Ahnung, wie die innere Klasse value_type
Ok, also wie würdest du diese Erklärung machen? –
Hoppla, ich dachte, Sie haben versucht, auf eine Mitgliedsvariable oder Funktion zuzugreifen, nicht auf einen Typ. – immibis