简体   繁体   中英

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();
            }
        }
    }
}
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.

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;

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