C++ : Using an array in a nested class definition (OOP)

标签 c++ arrays oop class

所以我试图定义一个类,我正在使用另一个不同类的数组来定义它。

  //header file for Name.h
class Name {
    string last;
    string first;
}; 

 //header file for Depositor.h
    class Depositor {
        Name name;
        string ssn;}; 



//header file for Account.h 
    class Account {
        Depositor depositor;
        int acctnum;
        string type;
        double balance;
    };

//header file for Bank.h
#include "Account.h"
class Bank {
    Account account[]; //is this possible?
    int active_accts;
};

当我编写 cpp 文件时遇到了很多问题!

//example of mutator
void Bank::setLastname(string lastname)
{
    account.setLastname (lastname);
}

我没有包含我写入头文件的修改器和访问器,但它们在那里并且是公开的——它不会编译。 你能帮我吗?在 Bank.h 中使用类的数组是否有效?

最佳答案

Is it even valid to use an array of a class in Bank.h?

是的,但它必须有一个固定的尺寸,例如,

Account account[3];

在 C++ 中,类型始终具有固定大小,并且由于数组成员变量构成类大小的一部分,因此您需要指定数组中有多少个元素。

如果您不知道需要多少元素,可以使用序列容器:

std::vector<Account> account;

关于C++ : Using an array in a nested class definition (OOP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5011546/

相关文章:

c++ - boost::bind 和 io_service 出现问题

php - 我可以通过执行两次来反转 diff_assoc_array() 吗?

ruby - 使用注入(inject)来计算数组中的元素

Perl 对象错误 : Can't locate object method via package

jquery类继承

c++ - 二进制数据 JSONCPP

c++ - 对象组合 - 无法访问对象属性

javascript - 从javascript中的构造函数创建编号对象

c++ - 重载 'operator++' 必须是一元或二元运算符(有 3 个参数)

java - 我应该如何将此对象添加到数组中...?