cmb
cmb

Reputation: 95

c++ overload [] operator

I am trying to overload the subscript operator [] in my class which uses a linked list to create a map. This and several variations, like adding const, is what I have tried.

header

int& operator[](std::string key);

and then defining the overload in a seperate file

int& mapLL::operator[](std::string key){ 
   int val = this->get(key);
   return val;
}

this is the error I don't know how to fix

main.cpp: In function ‘int main()’:
main.cpp:38:24: error: invalid types ‘mapLL*[const char [4]]’ for array subscript
 int a = list3["ghi"]; 
                    ^
mapLL.cpp: In member function ‘int& mapLL::operator[](std::string)’:
mapLL.cpp:110:9: warning: reference to local variable ‘val’ returned [-Wreturn-local-addr]
   int val = this->get(key);
       ^

Then in the main file I am trying this

mapLL *list3 = new mapLL();
list3->set("abc",1);
list3->set("def",2);
list3->set("ghi",3);
list3->set("jkl",1);
list3->toString();
cout << list3->get("ghi") << endl;
int a = list3["ghi"]; 
cout << a << endl;
delete list3;

get function

int mapLL::get(std::string key){
    bool found = false;
    node *me = (node *) first;
    if(is_empty()){
        return -2;
    }
    while(!found){
        if (me->getKey() == key){
            return me->getValue();
        }else{
            if (me->getNext() == 0){
                return -1;
            }else{
                me = (node *) me->getNext();
            }
        }
    }
}

Upvotes: 0

Views: 1662

Answers (2)

David Haim
David Haim

Reputation: 26496

int& mapLL::operator[](std::string key){ 
   int val = this->get(key);
   return val;
}

you are returning a reference to a local variable, val.
what you actually need to do is to find the element in you linked list and return it as is, no assignment to local variables in between.

Plus, list3 is a pointer, unfortunatly, you need to dereference it before using [] operator :

(*list3)["ghi"]; 

all have being said + looking at your profile, I get that you come from a Java background. my advice - understand what is the difference between stack allocation and heap allocation. this is the very basic of the language. you need to use dynamically allocated objects (=using new) very rarely.

although Java hides away allocation details, this is maybe the one of the most important subjects in C++. not everything has to be a pointer. your starting point is stack allocated objects. move from there to dynamic /static allocation if it does not line with your needs.

Upvotes: 4

Thomas Matthews
Thomas Matthews

Reputation: 57698

I recommend to refrain from using raw pointers and dynamic allocation. Your issue stems from incorrect use of pointers.

Use direct declarations:

mapLL list3;
list3.set("abc",1);
list3.set("def",2);
list3.set("ghi",3);
list3.set("jkl",1);
list3.toString();
cout << list3.get("ghi") << endl;
int a = list3["ghi"]; 
cout << a << endl;

Upvotes: 1

Related Questions