gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:20:55 26 4
gpt4 key购买 nike

该代码在main中可以正常工作,而没有用于2个类的单独文件,但是当我尝试将它们分开时,出现很多错误,提示‘ItemToPurchase’ was not declared in this scope。我以为是因为我在ItemToPurchase类中使用了ShoppingCart类,但无法识别实现的位置

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

}

}

最佳答案

这实际上是从底部开始并逐步解决问题的开始。首先,有了 header ,您通常会有 header 保护,有些人使用 #pragma,有些人使用#ifndef /#define /#endif 。我个人更喜欢后者,但是在以为标准的 #pragma的地方工作。因此,您通常遵循工作地点或所获告知的标准。

接下来的帮助是使用:

using namespace std;

当您最终污染 namespace 时,我个人不喜欢它,但是再次,它与标准和遵循编码惯例有关。通常,这只是避免在类名之前键入太多 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,因此需要包含该 header 。它还应包括其使用的标准类型和类的标题。通常,这是确保 header 不完全依赖于另一个 header 可能包含的 header 的最佳方法。
// 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头文件,并且都使用 #pragma一旦对其进行保护。

最后,我们将介绍主要功能,该功能将包含在.cpp文件中。现在,此函数通过名称同时命名ItemToPurchase和ShoppingCart,因此它应同时包含两个头文件。您可以仅包含ShoppingCart.h就可以了,因为它包含ItemToPurcahse.h,但是最好的做法是包括您所命名类型的所有 header 。

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

关于c++ - 如何将2个类分成单独的.h文件并正确设置它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60233909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com