C++ 在函数中使用具有结构的指针

C++ Using Pointers with structures in functions

我正在从事一个项目,在该项目中我应该使用结构和指针来组织客户的银行帐户信息。我在 initialize_CD_account 函数中遇到错误。

错误消息显示 "uninitialized local variable 'a' used"

我不知道是什么问题,感谢您的帮助!

#include <iostream>
using namespace std;

struct CDAccount
{
    double balance;
    double interest;
    int term;
};

struct CheckingAccount
{
    double balance;
    double interest;
};

struct CustomerInfo
{
    char * first_name;
    char * last_name;
};

struct Account
{
    CustomerInfo * cP;
    CheckingAccount * ckP;
    CDAccount * cdP;
};

void initialize(Account &);
void initialize_customer_info(CustomerInfo *);
void initialize_Checking_account(CheckingAccount *, double, double);
CDAccount * initialize_CD_account(double, double, int);
void update_customer_info(CustomerInfo *);
double calculate_total_balance(Account);

int main()
{
    Account account;

    initialize(account);

    cout << endl;
    cout << "The name of the customer is " << account.cP->first_name << " " << account.cP->last_name << endl;
    cout << "The balance in Checking Account is " << account.ckP->balance << endl;
    cout << "The balance in CD Account is " << account.cdP->balance << endl;
    cout << endl;

    update_customer_info(account.cP);
    double total_balance = calculate_total_balance(account);
    cout << endl;
    cout << "The name of the customer is changed to " << account.cP->first_name << " " << account.cP->last_name << endl;
    cout << "The total balance is " << total_balance << endl;

    system("pause");

    return 0;
}

void initialize(Account & a)
{
    a.cP = new CustomerInfo;
    initialize_customer_info(a.cP);
    a.ckP = new CheckingAccount;
    initialize_Checking_account(a.ckP, 2000, 0.02);
    a.cdP = initialize_CD_account(1000, 0.05, 5);
}

void initialize_customer_info(CustomerInfo * P)
{
    char * first = new char[];
    char * last = new char[];
    cout << "Enter customer's first name:";
    cin >> first;
    cout << "Enter customer's last name:";
    cin >> last;
    P->first_name = first;
    P->last_name = last;

    return;
}

void initialize_Checking_account(CheckingAccount * P, double b, double r)
{
    P->balance = b;
    P->interest = r;
}

CDAccount * initialize_CD_account(double b, double r, int t)
{
    CDAccount * a;
    a->balance = b;
    a->interest = r;
    a->term = t;
    return a;
}

void update_customer_info(CustomerInfo * P)
{
    char firstName[20];
    char lastName[20];
    cout << "Please enter the customer's new first name:" << endl;
    cin >> firstName;
    cout << "Please enter the customer's new last name:" << endl;
    cin >> lastName;
    P->first_name = firstName;
    P->last_name = lastName;
}

double calculate_total_balance(Account a)
{
    double total = 0;
    total = total + a.ckP->balance;
    total = total + a.cdP->balance;
    return total;
}

如错误所述,您的变量 a 未初始化。 您声明了它,但没有为它分配任何内存。 您可以使用 new 关键字来执行此操作:

CDAccount *a = new CDAccount;

这会为您的对象分配内存,然后将 a 设置为指向该对象的指针。

你在CDAccount * initialize_CD_account(double b, double r, int t)中的CDAccount * a;只是一个指针,没有为数据分配内存。

要解决问题,只需使用类似的东西:

 CDAccount *a = new CDAccount;

编辑:

此外,您在 void update_customer_info(CustomerInfo * P) 中的信息更新会遇到麻烦,因为您正尝试使用运算符 = 复制 char*(这是一种不好的做法,这个本地数组将数据存储在自动/堆栈中内存)。

只是不要使用局部数组firstName[20]lastName[20],例如:

void update_customer_info(CustomerInfo * P)
{
    cout << "Please enter the customer's new first name:" << endl;
    cin >> P->first_name;
    cout << "Please enter the customer's new last name:" << endl;
    cin >> P->last_name;
}

或使用函数复制字符串(例如 strncpy() )