Vector of C++ class type and .erase()

Asked 2 years ago, Updated 2 years ago, 26 views

In the update_quantity() of ShoppingCart.cpp, Using listOfItem.erase(p), if the name is the same and the newQuantity is less than 1, then the vector listOfItem is used You are about to erase elements of class item type. I know that vectors can be erased with erase, but I don't know why they can't. I would really appreciate your reply.

Error message and code below. There is also a file called cart_test.cpp, but I didn't upload it because it looked irrelevant.

ShoppingCart.h

#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <vector>

#include "Item.h"
class ShoppingCart
{
    public:
        void add(Item newItem);
        bool update_quantity(std::string name, int newQuantity);
        bool remove(std::string name);
        void write();
        double total_value();

    private:
        std::vector<Item> listOfItem;
};

#endif

ShoppingCart.cpp

#include "ShoppingCart.h"
#include <iostream>
#include <vector>

void ShoppingCart::add(Item newItem)
{
    bool trueFalse = false;

    if (newItem.get_quantity() > 0)    
    {
        for (auto& p : listOfItem)
        {
            if (p.get_name() == newItem.get_name())
            {
                p.set_quantity(p.get_quantity() + newItem.get_quantity());

                if (p.get_price() != newItem.get_price())
                {
                    p.set_price(newItem.get_price());
                };

                trueFalse = true;
            };
        };

        if (trueFalse == false)
            listOfItem.push_back(newItem);
    };

}
bool ShoppingCart::update_quantity(std::string name, int newQuantity)
{
    bool trueFalse;
    int position = 0;
    for (auto& p : listOfItem)
    {
        if (p.get_name() == name)
        {
            if (newQuantity < 1)
                {
                    listOfItem.erase(p);
                    trueFalse = false;
                }
            else
            {
                p.set_quantity(newQuantity);
                trueFalse = true;
            };


        };
    };
}

bool ShoppingCart::remove(std::string name)
{

}

void ShoppingCart::write()
{

}

double ShoppingCart::total_value()
{

}

Item.h

//file Item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>

class Item {
public:
    // // constructors
    Item(std::string name, double price, int quantity);
    // // accessors
    std::string get_name()     const;
    double      get_price()    const;
    int         get_quantity() const;
    // // mutators
    void set_price(double new_price);
    void set_quantity(int new_quantity);
    // // other methods
    void print() const; 

private:
    std::string name;     /// name of the item
    double      price;    /// price for one item
    int         quantity; /// how many are you buying?
};


#endif

Item.cpp

// @file Item.cpp
#include "Item.h"
#include <iomanip>
#include <iostream>

Item::Item( std::string name, double price, int quantity ) {
    this->name     = name;  // `this->` means "self"
    this->price    = price;
    this->quantity = quantity;
}

std::string Item::get_name( ) const {
    return name;
}

double Item::get_price( ) const {
    return price;
}

int Item::get_quantity( ) const {
    return quantity;
}

void Item::set_price( double new_price ) {
    price = new_price;
}

void Item::set_quantity( int new_quantity ) {
    quantity = new_quantity;
}

void Item::print( ) const {
    std::cout << std::fixed << std::setprecision( 2 );
    std::cout << name << '\n';
    std::cout << "\tPrice: $  " << price << '\n';
    std::cout << "\tQuantity: " << quantity << '\n';
}

c++

2022-09-20 18:58

1 Answers

In the code you posted, we put the element p of the vector directly into the erase function, but the erase function does not put the element directly, but the position of the element.

If you look at the code below, you'll get a sense.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> numbers{ 1,2,3,4 };

    cout << "Before deletion\n";
    for (int num : numbers)
        cout << num << '\n';

    for (int i = 0; i < (int)numbers.size(); ++i) {
        if (numbers[i] == 3) {
            numbers.erase(numbers.begin() + i);
            --i;
        }
    }

    cout << "\nDelete\n";
    for (int num : numbers)
        cout << num << '\n';


    return 0;
}

-Result


2022-09-20 18:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.