简体   繁体   中英

Inner class of operator[] overloading doesn't work

I'm making tree-like class, and want to support operator[] of which operand type is enum class.

My purpose to use operator[] with enum class is to access node's child ( ie node[Loc::left] - means left child of node) My node class is inner class of tree-like class and i think that make some problem below.

class Tree{   
   enum class Loc : uint8_t {left =0 , right = 1};
   class container{
              public:
                  container *parent, *left, *right;   
              container(){
                  this[Loc::left] = NULL; //this works...
              }
              container* operator[](Loc loc);
              const container* operator[](Loc loc) const;
   }
   container* operator[](Loc loc){
       return this->left;
   }   

   const container* operator[](Loc loc) const{
       return this->left;
   }

   ...
   void doSomething(){
        container *curr;
        if(curr[Loc::left] == NULL){ //this doesn't work
        }
   }
}   

Error message was "no match for 'operator[]' (operand types are 'Tree::container*' and 'Tree::Loc')"

I also tried to declare operator[] at out of class Tree, but nothing changed.

I can't understand what error message wants to me to do.

curr is a container* not a container . If you want to use [] on the container then you have to do (*curr)[Loc:left] .

Also, in your code curr is uninitialized. I assume this is just because it's an example, and your real code assigns something to loc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM