聚合关系 - 在 Private from Seperate .cpp/.h 中初始化 Class

Aggregate Relationship - Initializing Class within Private from Seperate .cpp/.h

所以我正在尝试创建一个银行账户程序,该程序实际上将使用两个独立的 类 派生自一个名为 statistics 的独立 .cpp/.h 程序(statistics.cpp / statistics.h) 我已将头文件包含在适当的位置,并确保我没有犯任何区分大小写的错误。向前进。我遇到的问题是我收到错误 -"warning: unused variable 'withdrawals' is not set." -"warning: unused variable 'deposits' is not set."

我已经在 statistics.cpp

中适当地创建了 constructor/overload 构造函数
 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 文件中,我 added/created 这两个单独的 类 试图建立聚合关系。

 //....
 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++;
}

您似乎想在 bankaccount::test() 中初始化 bankaccount 实例的 depositswithdrawals 成员。但是,Statistics withdrawals = Statistics(transactions); 只是创建一个局部变量并对其进行初始化。由于它从未被使用过,编译器会警告您可能打算做其他事情(比如初始化成员)。

您的意思可能是:

withdrawals = new Statistics(transactions);
deposits = new Statistics(transactions);

如果你这样做,你必须delete这些对象在bankaccount析构函数中以避免内存泄漏。现代 C++ 提供了更好的方法来做到这一点,但这离题了。

注意:Class用户代码中的名字通常在CamelCase,而不是alllowercase