所以我正在为我的 OO 课程做这个项目。我们需要创建两个类,Sale 和 Register。我写过销售和注册的大部分内容。我唯一的问题(目前)是我似乎无法从我的注册类访问销售对象的私有成员数据。
销售标头:
enum ItemType {BOOK, DVD, SOFTWARE, CREDIT};
class Sale
{
public:
Sale(); // default constructor,
// sets numerical member data to 0
void MakeSale(ItemType x, double amt);
ItemType Item(); // Returns the type of item in the sale
double Price(); // Returns the price of the sale
double Tax(); // Returns the amount of tax on the sale
double Total(); // Returns the total price of the sale
void Display(); // outputs sale info (described below)
private:
double price; // price of item or amount of credit
double tax; // amount of sales tax (does not apply to credit)
double total; // final price once tax is added in.
ItemType item; // transaction type
};
注册头:
class Register{
public:
Register(int ident, int amount);
~Register();
int GetID(){return identification;}
int GetAmount(){return amountMoney;}
void RingUpSale(ItemType item, int basePrice);
void ShowLast();
void ShowAll();
void Cancel();
int SalesTax(int n);
private:
int identification;
int amountMoney;
int listSize = 5;
int numSales;
Sale* sale;
};
所以我现在正在尝试编写RingUpSale()
函数,但我似乎无法访问私有字段。这是我的代码:
void Register::RingUpSale(ItemType item, int basePrice){
if(numSales == listSize){
listSize += 5;
Sale * tempArray = new Sale[listSize];
memcpy(tempArray, sale, numSales * sizeof(Sale));
delete [] sale;
sale = tempArray;
}
sale[numSales]->item = item; //this works for some reason
sale[numSales]->total = basePrice; // this doesn't
if(item == 'CREDIT'){
sale[numSales]->tax = 0; // and this doesn't
sale[numSales]->total = basePrice; // and neither does this
amountMoney -= basePrice;
}
++numSales;
}
尝试设置销售对象的总计和税收字段在 Eclipse 中出现错误:
"Field 'total' cannot be resolved"
我不确定这是为什么或如何解决它。任何帮助,将不胜感激。是的,我在必要时添加了#include "sale.h"
and 。#include "register.h"