为什么函数不能识别在 main() 中创建的对象?
Why the function doesn't recognize an object created in main()?
我必须为学校制作一个购物车程序,这个程序中的活动部分比我以前处理过的要多。我试图找出为什么我的 main.cpp
中的函数无法识别我在 main()
中创建的对象。它一直在说
the cart is not declared in the scope.
我仍然需要完成菜单的构建,但我什至不能让它识别在 main 中创建的对象。
我可以看到该对象正在正确创建,因为它可以在 main()
内进行操作没问题。此外,我什至在那里有一些占位符命令来让它工作。问题是家庭作业要求菜单在一个函数中。
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
using namespace std;
void PrintMenu()
{
while (true)
{
string choice;
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 item's descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl;
cout << endl;
cout << "Choose an option: " << endl;
cin >> choice;
if (choice == "a") {
cart.GetDate();
}
else if (choice == "d") {
}
else if (choice == "c") {
}
else if (choice == "i") {
}
else if (choice == "o") {
}
else if (choice == "q") {
break;
}
else {
cout << "That is not a valid choice" << endl;
}
}
}
int main()
{
string name;
string date;
cout << "Enter customer's name: " << endl;
cin >> name;
cout << "Enter today's date: " << endl;
cin >> date;
cout << endl;
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl;
ShoppingCart cart(name, date);
ItemToPurchase apple("apple", 1, 4, "apple");
cout << cart.GetDate();
cart.AddItem(apple);
cout << cart.GetNumItemsInCart();
PrintMenu();
}
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <string>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart
{
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector<ItemToPurchase> cartItems;
public:
ShoppingCart(string name, string date);
string GetCustomerName() const;
string GetDate();
void AddItem(ItemToPurchase);
void RemoveItem(string);
void ModifyItem();
int GetNumItemsInCart();
double GetCostofCart();
void PrintTotal();
string PrintDecriptions();
};
#endif
#include <iostream>
#include <string>
#include <vector>
#include "ShoppingCart.h"
using namespace std;
ShoppingCart::ShoppingCart (string name, string date){
customerName=name;
currentDate= date;
}
string ShoppingCart::GetCustomerName() const
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
void ShoppingCart::RemoveItem(string name)
{
for (int i = 0; i < cartItems.size(); i++)
{
if (cartItems.at(i).GetName() == name)
{
cartItems.erase(cartItems.begin() + i);
}
else
{
cout << "Item not found in cart. Nothing removed." << endl;
}
}
}
int ShoppingCart::GetNumItemsInCart(){
int number;
number = cartItems.size();
return number;
}
double ShoppingCart::GetCostofCart()
{
double sum = 0.0;
for (int i = 0; i < cartItems.size(); i++)
{
sum += cartItems[i].GetQuantity() * cartItems[i].GetPrice();
}
return sum;
}
#include "ItemToPurchase.h"
void ItemToPurchase::SetName(string SetItemName){
itemName = SetItemName;
}
void ItemToPurchase::SetPrice(int SetItemPrice){
itemPrice = SetItemPrice;
}
void ItemToPurchase::SetQuantity(int SetItemQuantity){
itemQuantity = SetItemQuantity;
}
string ItemToPurchase::GetName() const {
return itemName;
}
int ItemToPurchase::GetPrice() const {
return itemPrice;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
#include <string>
#include <iostream>
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase(string a, int b, int c, string d)
{itemName = a;
itemPrice = b;
itemQuantity = c;
itemDescription =d;
}
void SetName(string SetItemName);
void SetPrice(int SetItemPrice);
void PrintItemDescription();
void SetQuantity(int SetItemQuantity);
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
void SetDescription() const;
string GetDiscription() const;
void PrintItemCost() const;
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif
PrintMenu
中的这一行是问题所在:
cart.GetDate();
编译器在该函数 的范围内查找名为 cart
的东西,但它不存在。解决此问题的一种方法是将 reference 传递给 main
中创建的 cart
到 PrintMenu
函数:
void PrintMenu(ShoppingCart &cart){
并像这样调用函数:
PrintMenu(cart);
请注意,因为将来您要在菜单中修改 cart
,您需要将其作为 reference 传递(即&
) 而不是作为副本或常量引用。
[...], but I can't even get it, to recognize the object that gets created in main()
.
main()
和PrintMenu()
是两个不同的函数,作用域不同。一个人无法知道别人的变量,除非你以任何方式传递或让别人知道。
在你的情况下,你可以将 ShoppingCart
对象(即 cart
)从 main()
传递给 PrintMenu
函数,这样它就会知道哪个 cart
你的意思是:
void PrintMenu(ShoppingCart& cart)
// ^^^^^^^^^^^^^^^^^^^^
{
// ...
}
并使用 ShoppingCart
对象从 main()
调用函数。
PrintMenu(cart);
话虽这么说;
请不要用using namespace std;
练习。阅读更多:Why is "using namespace std;" considered bad practice?
如果成员函数不修改成员,则应将函数标记为const
。适用于 ItemToPurchase
和 ShoppingCart
类.
的所有 getter
当您 return 一个 non-trivial copyable objects 喜欢 getter 中的 std::string
时,您应该避免复制。这意味着您可能希望对所有 getters 进行如下更改,其中 returns std::string
:
const std::string& GetDate() const /* noexcept */
{
return currentDate;
}
我必须为学校制作一个购物车程序,这个程序中的活动部分比我以前处理过的要多。我试图找出为什么我的 main.cpp
中的函数无法识别我在 main()
中创建的对象。它一直在说
the cart is not declared in the scope.
我仍然需要完成菜单的构建,但我什至不能让它识别在 main 中创建的对象。
我可以看到该对象正在正确创建,因为它可以在 main()
内进行操作没问题。此外,我什至在那里有一些占位符命令来让它工作。问题是家庭作业要求菜单在一个函数中。
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
using namespace std;
void PrintMenu()
{
while (true)
{
string choice;
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 item's descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl;
cout << endl;
cout << "Choose an option: " << endl;
cin >> choice;
if (choice == "a") {
cart.GetDate();
}
else if (choice == "d") {
}
else if (choice == "c") {
}
else if (choice == "i") {
}
else if (choice == "o") {
}
else if (choice == "q") {
break;
}
else {
cout << "That is not a valid choice" << endl;
}
}
}
int main()
{
string name;
string date;
cout << "Enter customer's name: " << endl;
cin >> name;
cout << "Enter today's date: " << endl;
cin >> date;
cout << endl;
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl;
ShoppingCart cart(name, date);
ItemToPurchase apple("apple", 1, 4, "apple");
cout << cart.GetDate();
cart.AddItem(apple);
cout << cart.GetNumItemsInCart();
PrintMenu();
}
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <string>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart
{
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector<ItemToPurchase> cartItems;
public:
ShoppingCart(string name, string date);
string GetCustomerName() const;
string GetDate();
void AddItem(ItemToPurchase);
void RemoveItem(string);
void ModifyItem();
int GetNumItemsInCart();
double GetCostofCart();
void PrintTotal();
string PrintDecriptions();
};
#endif
#include <iostream>
#include <string>
#include <vector>
#include "ShoppingCart.h"
using namespace std;
ShoppingCart::ShoppingCart (string name, string date){
customerName=name;
currentDate= date;
}
string ShoppingCart::GetCustomerName() const
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
void ShoppingCart::RemoveItem(string name)
{
for (int i = 0; i < cartItems.size(); i++)
{
if (cartItems.at(i).GetName() == name)
{
cartItems.erase(cartItems.begin() + i);
}
else
{
cout << "Item not found in cart. Nothing removed." << endl;
}
}
}
int ShoppingCart::GetNumItemsInCart(){
int number;
number = cartItems.size();
return number;
}
double ShoppingCart::GetCostofCart()
{
double sum = 0.0;
for (int i = 0; i < cartItems.size(); i++)
{
sum += cartItems[i].GetQuantity() * cartItems[i].GetPrice();
}
return sum;
}
#include "ItemToPurchase.h"
void ItemToPurchase::SetName(string SetItemName){
itemName = SetItemName;
}
void ItemToPurchase::SetPrice(int SetItemPrice){
itemPrice = SetItemPrice;
}
void ItemToPurchase::SetQuantity(int SetItemQuantity){
itemQuantity = SetItemQuantity;
}
string ItemToPurchase::GetName() const {
return itemName;
}
int ItemToPurchase::GetPrice() const {
return itemPrice;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
#include <string>
#include <iostream>
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase(string a, int b, int c, string d)
{itemName = a;
itemPrice = b;
itemQuantity = c;
itemDescription =d;
}
void SetName(string SetItemName);
void SetPrice(int SetItemPrice);
void PrintItemDescription();
void SetQuantity(int SetItemQuantity);
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
void SetDescription() const;
string GetDiscription() const;
void PrintItemCost() const;
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif
PrintMenu
中的这一行是问题所在:
cart.GetDate();
编译器在该函数 的范围内查找名为 cart
的东西,但它不存在。解决此问题的一种方法是将 reference 传递给 main
中创建的 cart
到 PrintMenu
函数:
void PrintMenu(ShoppingCart &cart){
并像这样调用函数:
PrintMenu(cart);
请注意,因为将来您要在菜单中修改 cart
,您需要将其作为 reference 传递(即&
) 而不是作为副本或常量引用。
[...], but I can't even get it, to recognize the object that gets created in
main()
.
main()
和PrintMenu()
是两个不同的函数,作用域不同。一个人无法知道别人的变量,除非你以任何方式传递或让别人知道。
在你的情况下,你可以将 ShoppingCart
对象(即 cart
)从 main()
传递给 PrintMenu
函数,这样它就会知道哪个 cart
你的意思是:
void PrintMenu(ShoppingCart& cart)
// ^^^^^^^^^^^^^^^^^^^^
{
// ...
}
并使用 ShoppingCart
对象从 main()
调用函数。
PrintMenu(cart);
话虽这么说;
请不要用
using namespace std;
练习。阅读更多:Why is "using namespace std;" considered bad practice?如果成员函数不修改成员,则应将函数标记为
的所有 getterconst
。适用于ItemToPurchase
和ShoppingCart
类.当您 return 一个 non-trivial copyable objects 喜欢 getter 中的
std::string
时,您应该避免复制。这意味着您可能希望对所有 getters 进行如下更改,其中 returnsstd::string
:const std::string& GetDate() const /* noexcept */ { return currentDate; }