简体   繁体   中英

error C2440: 'initializing' : cannot convert from 'const int' to 'int *'

When I try to run this code

template <typename Item, typename Key>
Item* BST<Item,Key>::search(const Key& key) const {
    for (std::size_t i = 0; i < tree.size(); ++i) {
                Item* ptr = NULL;
        if(tree[i].data == key && tree[i].occupied == true) {
            ptr = tree[i].data;
            return ptr;
        }
    }
    return ptr;
}

I end up with the error described in the title. I've tried multiple times to fix it but can't figure out what I am doing wrong. For some background info, tree is a vector of nodes. Each node has a boolean variable stating whether it is occupied and an Item variable called data. The function is supposed to search the vector for a node with the given key and return a pointer to it's data but I can't figure it out for the life of me.

Looks like the type of tree[i].data is const int and not int* . The code should store its address into ptr and maybe return const Item* . Even better, return an iterator that points to the node or the end-of-sequence iterator if the node wasn't found.

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