title: How to copy one object from a vector to another vector in C++?
body: <p>I have a Product class which represents a single product object, each new Product object is stored inside a vector that is part of the Catalog class. I want the user to be able to add products that exist inside the catalog vector to his shopping cart, which is another class that has a vector to save the products he wants to order. I want this to be done by copying from the Catalog vector to the shopping cart vector.</p>
  
  <p><strong>Product class</strong></p>
  
  <pre><code>class Product
  {
      private:
          // int id;
          // Category category;
          std::string name;
          std::string description;
          float price;
          unsigned short int stock; 
  
      public:
          Product(std::string name, std::string description, float price, 
                  unsigned short int stock);
  
          void setId(int id);
          // void setCategory(Category category);
          void setName(std::string name);
          void setDescription(std::string description);
          void setPrice(float price);
          void setStock(unsigned short int stock);
  
          int getId();
          // Category getCategory();
          std::string getName();
          std::string getDescription();
          float getPrice();
  };
  
  </code></pre>
  
  <p><strong>Catalog class</strong></p>
  
  <pre><code>class Catalog 
  {
      // friend class ShoppingCart;
  
      private:
          std::vector&lt;Product&gt; catalog;
          std::vector&lt;Product&gt;::iterator it;
  
      public:
          /**
           * TODO: The createProduct(), deleteProduct() and updateProduct() method should only be accessible by the admin user
           */
  
          std::vector&lt;Product&gt; getCatalog();
          // add product to catalog -&gt; admin
          void productCreate(Product p);
          // delete product from catalog -&gt; admin
          void productDelete(std::string name);
          // update a product -&gt; admin
          void productUpdate(std::string &amp;name);
          // list products inside catalog
  
          void productList();
          // search products 
          bool productSearch(std::string name);
  
  };
  </code></pre>
  
  <p><strong>ShoppingCart class</strong></p>
  
  <pre><code>class ShoppingCart
  {   
      private:
          // vector that contains products to order
          std::vector&lt;Product&gt; shoppingCart;
          Catalog catalog;
          // int quantity;
  
      public:
          void cartList();
          int cartSize();
          void addToCart(std::string);
          void deleteFromCart(std::string);
          void clearCart();
  };
  </code></pre>
  
  <p>Since this project is text based (terminal), I want the user to be able to add the product to his shopping cart by typing the product name. My logic for this can be seen in the code below:</p>
  
  <p><strong>addToCart method from ShoppingCart class</strong></p>
  
  <pre><code>void ShoppingCart::addToCart(string name)
  {   
      /** 
       * BUG: Doesnt add objects to the shoppingcart vector
       */
  
      for (Product p : catalog.getCatalog())
      {
          if (p.getName() == name) {
              shoppingCart.push_back(p);
          } else {
              printf("Oops.. %s doesn't seem to exist in our catalog.",
                      name.c_str());
          }
      }
  }
  </code></pre>
  
  <p><strong>View that receives the user input</strong></p>
  
  <pre><code>void View::shoppingcart()
  {
      string productName;
  
      cout &lt;&lt; "Add to cart: ";
      cin &gt;&gt; productName;
  
      cart.addToCart(productName);
  
      printf("You added '%s'", productName.c_str());
  
      cout &lt;&lt; endl;
  }
  </code></pre>
  
  <p>when I execute this code no errors are given but the nothing actually gets added to the shopping cart vector, size stays at 0. What am doing wrong? Or am I missing something here? Keep in mind that im new to C++ programming.</p>