我希望Account类保留一个人的名字,姓氏及其余额
Produce类包含有关水果或蔬菜类型的信息
我希望Account类中的SellProd函数接受一个Produce对象,以便它可以将该Produce的价格添加到该人的余额中。
到目前为止,该函数给我一个错误,提示“未知类型名称'Produce'
任何提示,使它工作?
#include <iostream>
#include <string>
using namespace std;
class Account
{
friend class Produce;
private:
double funds;
string ownerfirst, ownerlast;
void addToFunds(double p) { funds += p;};
public:
Account( string first, string last) { ownerfirst = first; ownerlast = last; funds = 0;};
void printAccount()
{
cout<<"Account Holder: "<<ownerfirst<<" "<<ownerlast<<endl;
cout<<"Account Balance: "<<funds<<endl;
};
void sellProd(Produce a)
{
cout<<"Selling: "<<kind<<" | Classified as: "<<type<<endl;
cout<<"Current Balance: "<<funds<<endl<<"Sell price: "<<price<<endl;
addToFunds(price);
cout<<"New Balance: "<<funds<<endl;
};
};
class Produce
{
private:
string kind;
string type; //fruit or vegetable
double price;
public:
void printProd()
{
cout<<"Type of "<<type<<": "<<kind<<endl;
cout<<"Selling for: "<<price<<"$"<<endl;
};
Produce( string k, string t, double p) { kind = k; type = t; price = p;};
};
int main()
{
Account myAccount("John", "Doe");
myAccount.printAccount();
Produce prod1("Tomato", "Fruit", 2.99);
Produce prod2("Apple", "Fruit", 0.99);
Produce prod3("Carrots", "Vegetable", 1.49);
Produce prod4("Potato", "Vegetable", 1.29);
prod1.printProd();
myAccount.printAccount();
}
最佳答案
Produce
是在Account
之后定义的,因此,当编译器看到函数的定义时,它还没有看到Produce
是什么。只需切换定义,使Produce
在Account
之前。