0

我正在尝试在 C++ 中的类中声明一个成员对象,如下所示:

class bankAccount
{
public:
  bankAccount(int, int, int, string, int);
private:
  bankBranch homeBranch;
};

bankAccount::bankAccount(int accountNumber, int accountBalance, int homebsbnumber, 
string homeaddress, int homepostcode) : homeBranch(homebsbnumber, homeaddress, homepostcode) {}
class bankBranch
{
public:
/** bankBranch(const int& bsbNumber, const string& address, const int& postCode) 
{ 
    this->bsbNumber = bsbNumber;
    this->address = address;
    this->postCode = postCode;
}; **/

bankBranch(int, string, int);
void setPostCode() 
{ 
    cout << "Please type in the postal code of your bankBranch: " << endl;
    cin >> postCode;
}
void setBsbNumber()
{ 
    cout << "Please type in the BSB Number of your bankBranch: " << endl;
    cin >> bsbNumber;

}
void setAddress() 
{ 
    cout << "Please type in the address of your bankBranch " << endl;
    cin >> address;
}
// Return methods for bsb number, address and post code
int getBsbNumber() 
{
    return this->bsbNumber;
}
string getAddress() 
{
    return this->address;
}
int getPostCode()
{ 
    return this->postCode;
}
private:
  int bsbNumber;
  string address;
  int postCode;
};
bankBranch::bankBranch(int bsbnum, string bankaddress, int bankpostcode) { 

bsbNumber = bsbnum;
address = bankaddress;
postCode = bankpostcode;

}

我收到这些错误:

  • 语法错误:缺少';' 在标识符“homeBranch”之前
  • 缺少类型说明符 - 假定为 int(因为它显然是一个对象而感到困惑?)
  • “bankAccount”:非法成员初始化:“homebranch”不是基础或成员

我已经尝试了很多事情,但似乎无法修复这些错误,谁能提供一些关于如何将成员对象 bankBranch homeBranch 正确添加到类 bankAccount 的见解?以及如何正确使用初始化列表。

4

1 回答 1

2

您需要包含bankBranch在此文件中定义的头文件(而不是翻译单元)。

由于您在使用组合和创建bankBranch编译器成员时需要了解布局的详细信息以及bankBranch实例所需的内存,因此只能通过了解bankBranch. 请注意,前向声明在这里是不够的。

于 2013-05-25T14:39:02.327 回答