Reputation: 25
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 ( i.e 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.
Upvotes: 0
Views: 63
Reputation: 58858
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
.
Upvotes: 4