lundi 28 octobre 2019

Ambiguous overloading with operator[] and operator int()

I am creating a class Item, and each Item is a key/value pair. Also, each Item may also contain subitems:

#include <string>
#include <vector>
#include <iostream>


class Item
{
    private:
        std::string key;
        unsigned int value;
        std::vector<Item> subitems;


    public:
        Item( const std::string& key = "", const int& value = 0 )
        : key( key ), value( value ){ };


    public:
        // Search or Create new SubItem.
        Item& operator[]( const std::string& key )
        {
            for( auto& subitem : subitems )
                if( subitem.key == key )
                    return subitem;

            subitems.push_back( Item( key ));
            return subitems.back( );
        }


    public:
        // Assign new value to Item.
        Item& operator=( const int& value )
        {
            this->value = value;
            return *this;
        }


    public:
        // Get value from Item.
        operator unsigned int( ) const
        {
            return value;
        }
};



int main( void )
{
    Item item;


    item["sub"] = 42;
    unsigned int sub = item["sub"];


    std::cout << std::to_string( sub ) << std::endl;
    return 0;
}

When I try to compile this, I get:

error: ambiguous overload for ‘operator[]’ (operand types are ‘Item’ and ‘const char [4]’)

If I create a member method unsigned int Get() instead of operator int() it does compile. But I wanted the class to work the same way that std::map works:

#include <map>
#include <string>
#include <iostream>



int main( void )
{
    std::map<std::string, unsigned int> item;


    item["sub"] = 42;
    unsigned int sub = item["sub"];


    std::cout << std::to_string( sub ) << std::endl;
    return 0;
}

How can I get it working? Thanks!

Aucun commentaire:

Enregistrer un commentaire