0

I've initialized two maps:

map<int, item> items
map<item, int> cart

See the following:

item milk(allSKU[0], "Horizon Milk", 50, 4.99);
item phone(allSKU[1], "iPhone 13", 50, 799.99);
item apples(allSKU[2], "Fuji Apples (6ct)", 50, 3.99);
item webcam(allSKU[3], "Webcam 1080p", 50, 49.99);
item ps5(allSKU[4], "PlayStation 5 Digital", 50, 399.99);

map<int, item> items = { { allSKU[0], milk },
                         { allSKU[1], phone },
                         { allSKU[2], apples },
                         { allSKU[3], webcam },
                         { allSKU[4], ps5 } };
display(Store);

map<item, int> cart;

int user_input; 
bool still_buying = true;
while (still_buying) {
    cout << "  Please scan the SKU of the item (Press 0 to finish): ";
    cin >> user_input;
    if (user_input == 0) {
        break;
    } // Error here?
    else if (std::find(allSKU.begin(), allSKU.end(), user_input) != allSKU.end()) {
        
        // if item is already in the cart, dont add it. 
        // find the item's qty and +1
        map<item, int>::iterator it = cart.find(items.at(user_input));
        if (cart.end() != it) {
            cart[items.at(user_input)]++;
        }
        else { 
            // if input is new, add item name and set qty to 1
            cart.insert(pair<item, int>(items.at(user_input), 1));
        }
        cout << "  " << items.at(user_input).getName() << " scanned!\n";
    }
    else {
        cout << "\n  This item doesnt exist!";
    } // throw exception if input not an integer

}

However, I run into an error

binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator

Essentially, I created a map called cart, with two types: an item object as a key and an int; its quantity as its value. Every time a user scans a SKU, it should first check if the item is in the cart, and if it is, increment its quantity. If it's unique, then insert it and set its quantity to 1.

I believe my issue is under my else if statement, but I cannot figure out why.

Any help is appreciated.

swangp2
  • 1
  • 1
  • You should post a [mcve], i.e. a simple, full 3 or 4 line program that duplicates the error. 99% of what you posted is totally irrelevant to the compiler error. – PaulMcKenzie May 08 '22 at 22:24
  • Hm, sorry. I only just started getting this error once I started using a map. I'll try to get one up. – swangp2 May 08 '22 at 22:25
  • If you are asking for help, you should make it easy for people to help you. The code as written doesn't even come close to compiling. You didn't even tell us which line produces the error. My psychic powers tell me that it's at the `cart[items.at(user_input)]++`, and the reason is that your `item` does not implement `operator<`. – Raymond Chen May 08 '22 at 22:31
  • `std::map` is sorted by key. To sort, it needs `operator<` that takes two items of the type `Key`. If you want to have ` std::map`, there needs to be a `bool operator<(const item&, const item&)` that would tell compiler which `item` is "less than" other `item`. – Yksisarvinen May 08 '22 at 22:32
  • @RaymondChen @Yksisarvinen I see, thank you! Apologies, still new to this stuff. I believe my woes will be fixed if I use an `unordered_map`. Will try. Otherwise, I'll implement `operator<` – swangp2 May 08 '22 at 22:35
  • 3
    @swangp2 -- [Here is a minimal example](https://godbolt.org/z/5zMMhYnfW). The way a programmer figures out a compiler error is to eliminate all of the noise in the code, and write a tiny, full example to see what the issue is. There is no `allSKU`, no user input, no output statements, no `while` or `for` loops -- just a simple "here is an item class, here is a map, and an attempt to insert an item into the map". – PaulMcKenzie May 08 '22 at 22:36
  • `std::unordered_map` needs a hash function for the key, so it's still something to implement, but a very different thing. I'm not sure about your use case, but `std::map` seems like a good choice here. `std::vector>` could be an alternative, but accessing items is going to be more cumbersome (you will need to iterate every time to get the right `item`) – Yksisarvinen May 08 '22 at 22:40

0 Answers0