所以我试图创建一个银行账户程序,它本质上将使用两个独立的类,这些类从一个名为统计的单独 .cpp/.h 程序(statistics.cpp/statistics.h)派生我已将头文件包含在适当的位置并确保我没有犯任何区分大小写的错误。向前进。我遇到的问题是我收到一个错误-“警告:未设置未使用的变量'withdrawals'。” -“警告:未设置未使用的变量‘存款’。”
我已经在我的 statistics.cpp 中适当地创建了我的构造函数/重载构造函数
Statistics::Statistics(){
size = 0;
pData = nullptr;
capacity = 0;
}
Statistics::Statistics(int capacity){
pData = new double [capacity];
size = 0;
//int i = 0;
}
以及在我的 statistics.h 文件中定义它
class Statistics{
public:
Statistics();//default constructor
Statistics(int capacity);//...
在我的 bankaccount.h 文件中,我添加/创建了两个单独的类以尝试建立聚合关系。
//....
private:
double amount = 0;
int transactions = 0;
double value = 0;
Statistics* deposits;
Statistics* withdrawals;
};
#endif
这就是我在 bankaccount.cpp 中初始化和设置类的方式
int main(){
//Statistics aStatistics; Constructor of Statistics
//aStatistics.test(); Test function for Statistics .cpp/.h files (Was
//successful)
bankaccount abankaccount;
abankaccount.test();
return 0;
}
void bankaccount::test(){
cout << "Hello, Welcome to Boca Regional Bank." << endl;
cout << "**********************************************" << endl;
cout << "What is the expected amount of transactions for this month?"
<<endl;
cin>> transactions;
Statistics withdrawals = Statistics(transactions); //"Warning: unused variable 'withdrawals'"
Statistics deposits = Statistics(transactions); //"Warning: unused variable 'deposits'"
int userchoice;
do{
cout << "\n";
userchoice = getuseroption();
process(userchoice,transactions);
} while (userchoice != 0);
}
void bankaccount::process(int option, int transactions){
switch (option){
case 1:
double temp1;
cout <<endl;
cout << "You've chosen to deposit into your Bank Account."
<< endl;
cout << "Your current balance is " << amount << endl;
cout << "How much would you like to deposit?" << endl;
cin >> temp1;
while ((temp1 < 0) || (temp1 > 100000) ){
cout << "You've entered an invalid amount, please try
again."<<endl;
cout << "How many would you like to deposit?"<<endl;
cin >> temp1;
}
deposit(&amount,temp1);
if (temp1 > 0){
deposits->add(temp1); //Implementation of one of the functions
//from the statistics class
//At this point the program crashes
}
else if (temp1 == 0){
break;
}
break;
....}
这是统计 .cpp/.h add(); 功能
void Statistics::add (double value){
pData[size]=value;
size++;
}