-1

所以我试图创建一个银行账户程序,它本质上将使用两个独立的类,这些类从一个名为统计的单独 .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++;
}
4

1 回答 1

0

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

你的意思可能是:

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

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

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

于 2018-04-12T15:25:22.793 回答