我怎样才能让一个类函数接受另一个对象? (C ++)

时间:2015-12-04 02:20:32

标签: c++ object

我希望帐户类能够保存一个人的姓氏和余额

Produce类保存有关某种水果或蔬菜的信息

我希望Account类中的SellProd函数接受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();

}

2 个答案:

答案 0 :(得分:1)

Produce是在Account之后定义的,所以当编译器看到函数的定义时,它没有看到Produce是什么。只需切换定义,Produce就会出现Account

答案 1 :(得分:0)

您似乎希望您的Account类包含Produce对象。在我的示例中,我假设您已将Produce类移动到单独的.h和.cpp文件中,并在该类的实现中包含一些getter函数。它可能看起来像这样:

#include <iostream>
#include <string>
#include "Produce.h" // including the assumed files mentioned above
using namespace std;
class Account
{
    friend class Produce;
private:
    double funds;
    string ownerfirst, ownerlast;
    void addToFunds(double p) { funds += p;};
    Produce a; //creating a produce object for your account class
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() //since Produce a is a private data member of your
    //class, you do not need to include it in functions that your 
    //class implements.     
    {
        cout<<"Selling: "<<a.getKind()<<" | Classified as: "<<a.getType()<<endl;
        cout<<"Current Balance: "<<funds<<endl<<"Sell price: "<<a.getPrice()<<endl;
        addToFunds(a.getPrice());
        cout<<"New Balance: "<<funds<<endl;
    };

您的Produce类的getter函数可以很简单(这只是实现的一个示例,或.cpp文件):

using namespace std;

class Produce(typeInput,priceInput,kindInput)

Produce::Produce() {
type=typeInput;
price=priceInput;
kind=kindInput;
}

Produce::~Produce(){}

int Produce::getPrice(){
 return price;
}