gpt4 book ai didi

c++ - Adding Pointer results in error to class 错误重载的成员函数未找到

转载 作者:行者123 更新时间:2023-11-28 03:34:26 27 4
gpt4 key购买 nike

我的代码有一些问题。具体来说:

bool Customer::checkout(Inventory* inv) {
double total = 0;
for( unsigned int i=0; i < _Cart.size(); i++ ) {
total += _Cart[i].price; // price * quantity??
}
if( Customer.balance < total ) {
return false;
}
else {
unsigned int i =0;
for ( i =0; i < inv->_Inv.size(); i++) {//go through inventory, check names, if there is a match,
if (inv->_Inv[i].name == _Cart[i].name) { //decrement the quantity accordingly, if there is no match,
inv->_Inv[i].quant -= _Cart[i].quant;
break;
}
}
if( i == inv->_Inv.size() ) { //you need to push the new food into the inventory vector
inv->_Inv.push_back(_Cart[i]);
return true;
}
}

在我的头文件中我有:

class Customer {
public:
Customer( string n, int c, double b );
bool addCart( Food f );
bool removeCart( Food f );
void report(); //to do
bool checkout(Inventory* inv); //to do
protected:
string name;
int card;
double balance;
//CreditCard _CC;
vector<Food> _Cart;
};

class Inventory {
public:
vector<Food> _Inv;
vector<Food> _Purchases;
int interval;
void firststock( string fileName );
void showFirststock( string fileName);
void restock( string file, int inter );
void summary(); //to do

我得到的错误如下:

  1. 错误 C2061:语法错误:标识符“库存”错误 C2061:(关于 bool checkout(Inventory* inv);)
  2. 语法错误:标识符 'Inventory' bool
  3. Customer::checkout(Inventory )':重载成员函数不在“客户”中找到,请参阅“客户”的声明....(关于 bool Customer::checkout(Inventory inv))

请帮忙

最佳答案

Customer 的定义之前,您需要对 Inventory 进行前向声明。

class Inventory;
class Customer {
//....
};

class Inventory {
//....
};

代码的一些注释:

  • 通过 const 引用传递复杂数据类型 - 例如 - bool addCart( const Food& f ); 而不是 bool addCart( Food f );

  • 您使用不合格的 string 表明您在 header 中的某处有 using namespace std;。这不行,至少在 header 中,您不应该使用 using 指令,而是完全限定类型 - std::string,它也应该通过引用传递

关于c++ - Adding Pointer results in error to class 错误重载的成员函数未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488167/

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