如何将 2 个类分成单独的 .h 文件并正确设置它们

how to seperate 2 classes into seperate .h files and properly set them up

本文关键字:文件 设置 单独      更新时间:2023-10-16

代码在main中工作正常,没有2个类的单独文件,但是当我尝试将它们分开时,我收到许多错误说‘ItemToPurchase’ was not declared in this scope。我假设这是因为我在类中使用ItemToPurchaseShoppingCart但我无法让它识别实现的位置

我在ShoppingCart.h文件中尝试了这样的东西(如下(,但它没有效果,我不确定我是否错过了其他错误的东西。

#ifndef ShoppingCart_h
#define ShoppingCart_h
#ifndef ItemToPurchase_h
#define ItemToPurchase_h

我在下面发布了工作代码

我不确定如何为 5 个文件、main和 4 个.h文件设置文件头

如果有人可以展示如何设置标题,那将是理想的选择,我是一名学生,在这一点上有点迷茫

#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;
}
};

//---------------------------------------------------------------

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;
}
};

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;
}
}
}

这真的是一个从底部开始并逐步解决问题。 首先,对于标题,您通常会有一个标头保护,有些人使用#pragma,一旦有些人使用#ifndef/#define/#endif。 我个人更喜欢后者,但在一个#pragma 一次是标准的地方工作。 所以你通常遵循你在哪里工作或你被告知的标准。

下一点帮助是使用:

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; }
};

作为一点旁注,您可能应该限定 Get 方法。 无论如何,上述内容可以放入ItemToPurchase.h头文件中,因为它包含所需的一切。

接下来是购物车类。 这专门命名了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.hItemToPurchase.h头文件,两者都受到#pragma 保护。

最后,我们进入我们的函数,它将包含在一个.cpp文件中。 现在,此函数按名称命名ItemToPurchaseShoppingCart,因此它应该包含两个头文件。 你可以只包括ShoppingCart.h,因为它包括ItemToPurcahse.h,但最好包括你命名的所有类型的标题。

最后,这为您提供了一个主文件.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 文件中。