聚合关系 - 从单独的.cpp / .h初始化Private内的类

问题描述 投票:-1回答:1

所以我试图创建一个bankaccount程序,它实际上将使用两个独立的类派生自一个名为statistics(statistics.cpp / statistics.h)的单独的.cpp / .h程序。我已将头文件包含在适当的位置并确保我没有出现任何区分大小写的错误。向前进。我遇到的问题是我收到了错误 - “警告:未设置未使用的变量'撤销'。” - “警告:未设置未使用的变量'存款'。”

我在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;
....}

这是Statistics .cpp / .h add();功能

void Statistics::add (double value){
    pData[size]=value;
    size++;
}
c++ class aggregate-initialization
1个回答
0
投票

看起来你想在deposits初始化你的withdrawals实例的bankaccountbankaccount::test()成员。但是,Statistics withdrawals = Statistics(transactions);只是创建一个局部变量并初始化它。由于它从未使用过,编译器警告您可能打算做其他事情(比如初始化成员)。

你的意思是:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.