Это действительно начало внизу и решение вашей проблемы. Во-первых, с заголовком у вас обычно есть защита заголовка, некоторые люди используют # pragma один раз некоторые люди используют # ifndef / # define / # endif . Я предпочитаю последнее лично, но работаю в месте, где # pragma Once является стандартом. Таким образом, вы обычно следуете стандартам, в которых вы работаете, или тем, что вам сказали.
Следующая помощь - это использование:
using namespace std;
Мне лично не нравится это в конечном итоге загрязняет пространство имен, но, опять же, речь идет о стандартах и следовании практикам кодирования. Обычно это всего лишь способ избежать необходимости набирать слишком много std :: перед именем класса.
Сказав это, давайте перейдем к коду ... Внизу вы иметь ItemToPurchase это не включает в себя никаких других классов, кроме стандартных C ++, так что в итоге вы получите:
// ItemToPurchase.h
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ItemToPurchase
{
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
public:
ItemToPurchase()
{
itemName = "";
itemPrice = 0;
itemQuantity = 0;
itemDescription = "none";
}
ItemToPurchase(string name, string description, int price, int quantity)
{
itemName = name;
itemDescription = description;
itemPrice = price;
itemQuantity = quantity;
}
void SetName(string name) { itemName = name; }
void SetPrice(int price) { itemPrice = price; }
void SetQuantity(int quantity) { itemQuantity = quantity; }
string GetName() { return itemName; }
int GetPrice() { return itemPrice; }
int GetQuantity() { return itemQuantity; }
void SetDescription(string description) { itemDescription = description; }
string GetDescription() { return itemDescription; }
void PrintItemCost() {
cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << itemQuantity * itemPrice << endl; }
void PrintItemDescription() { cout << itemName << ": " << itemDescription << endl; }
};
Так же, как небольшая дополнительная заметка, вы, возможно, должны const квалифицировать Get методы. В любом случае, вышеперечисленное можно поместить в заголовочный файл ItemToPurchase.h , поскольку он содержит все, что ему нужно.
Далее у вас есть класс ShoppingCart . В частности, это имя ItemToPurchase , поэтому необходимо включить этот заголовок. Он также должен включать заголовки для стандартных типов и классов, которые он использует. Обычно это лучший способ убедиться, что заголовок не полностью зависит от заголовка, который мог включать другой заголовок.
// ShoppingCart.h
#pragma once
#include "ItemToPurchase.h"
#include <vector>
#include <iostream>
#include <string>
class ShoppingCart
{
private:
string customerName;
string currentDate;
vector <ItemToPurchase> cartItems;
public:
ShoppingCart()
{
customerName = "none";
currentDate = "January 1, 2016";
}
ShoppingCart(string name, string date)
{
customerName = name;
currentDate = date;
}
string GetCustomerName() { return customerName; }
string GetDate() { return currentDate; }
void AddItem(ItemToPurchase newItem) { cartItems.push_back(newItem); }
void RemoveItem(string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems.erase(cartItems.begin() + i);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing removed." << endl << endl;
}
else
{
cout << endl;
}
}
void ModifyItem(int quantity, string name)
{
bool temp = true;
for (int i = 0; i < cartItems.size(); i++)
{
if (name == cartItems[i].GetName())
{
cartItems[i].SetQuantity(quantity);
temp = false;
}
}
if (temp == true)
{
cout << "Item not found in cart. Nothing modified." << endl << endl;
}
else
{
cout << endl;
}
}
int GetNumItemsInCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetQuantity();
}
return total;
}
int GetCostOfCart()
{
int total = 0;
for (int i = 0; i < cartItems.size(); i++)
{
total += cartItems[i].GetPrice() * cartItems[i].GetQuantity();
}
return total;
}
void PrintTotal()
{
if (cartItems.size() == 0)
{
cout << GetCustomerName() << "'s Shopping Cart - " << GetDate() << endl;
cout << "Number of Items: 0" << endl << endl;
cout << "SHOPPING CART IS EMPTY" << endl << endl;
cout << "Total: $0" << endl << endl;
}
else
{
cout << customerName << "'s Shopping Cart - " << currentDate << endl;
cout << "Number of Items: " << GetNumItemsInCart() << endl << endl;
for (int i = 0; i < cartItems.size(); i++)
{
cartItems[i].PrintItemCost();
}
cout << endl << "Total: $" << GetCostOfCart() << endl << endl;
}
}
void PrintDescriptions()
{
cout << "Item Descriptions" << endl;
for (int i = 0; i < cartItems.size(); i++)
{
cartItems[i].PrintItemDescription();
}
cout << endl;
}
};
Теперь мы закончили с ShoppingCart.h и заголовочные файлы ItemToPurchase.h и оба защищены # прагмой один раз .
Наконец, мы переходим к нашей main функции который будет содержаться в файле. cpp. Теперь эта функция называет имена ItemToPurchase и ShoppingCart по имени, поэтому она должна включать оба заголовочных файла. Вы можете просто включить ShoppingCart.h , поскольку он включает ItemToPurcahse.h , но рекомендуется включать все заголовки указанных вами типов.
Наконец, это дает вам main. cpp файл, который должен быть:
// main.cpp
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
#include <iostream>
#include <string>
int main()
{
cout << "Enter customer's name:" << endl;
string name;
getline(cin, name);
cout << "Enter today's date:" << endl << endl;
string date;
getline(cin, date);
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl << endl;
ShoppingCart customer(name, date);
bool on = true;
bool check = true;
string select;
while (on)
{
cout << "MENU" << endl;
cout << "a - Add item to cart" << endl;
cout << "d - Remove item from cart" << endl;
cout << "c - Change item quantity" << endl;
cout << "i - Output items' descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option:" << endl;
cin >> select;
while (check)
{
if (select == "a" || select == "d" || select == "c" || select == "i" || select == "o" || select == "q")
{
check = false;
}
else
{
cout << "Choose an option:" << endl;
cin >> select;
}
}
check = true;
if (select == "a")
{
cout << "ADD ITEM TO CART" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the item description:" << endl;
string description;
getline(cin, description);
cout << "Enter the item price:" << endl;
int price;
cin >> price;
cout << "Enter the item quantity:" << endl << endl;
int quantity;
cin >> quantity;
ItemToPurchase temp(name, description, price, quantity);
customer.AddItem(temp);
}
else if (select == "d")
{
cout << "REMOVE ITEM FROM CART" << endl;
cout << "Enter name of item to remove:" << endl;
cin.ignore();
string name;
getline(cin, name);
customer.RemoveItem(name);
}
else if (select == "c")
{
cout << "CHANGE ITEM QUANTITY" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
string name;
getline(cin, name);
cout << "Enter the new quantity:" << endl;
int quantity;
cin >> quantity;
customer.ModifyItem(quantity, name);
}
else if (select == "i")
{
cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;
cout << customer.GetCustomerName() << "'s Shopping Cart - " << customer.GetDate() << endl << endl;
customer.PrintDescriptions();
}
else if (select == "o")
{
cout << "OUTPUT SHOPPING CART" << endl;
customer.PrintTotal();
}
else if (select == "q")
{
on = false;
}
}
}
И все это должно работать, с вашими определениями классов, выделенными в соответствующие им файлы .h.