C++ 将数据输入私有向量时出现问题(无效使用)

2024-01-11

我有一个“Account”类的向量。它是 BankingSystem 类私有的。这是我如何定义它们的。

账户类别:

struct newAccount
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;

}; //end of structure newAccount

class Account
{
    string firstName;
    string lastName;
    string accountPass;
    int accountID;
    float accountBalance;

private:
    int depositAmount;
    int withdrawAmount;

public:
    static newAccount createAccount( int, float, string, string, string );  //creates new account
    void deposit( int );    //deposits money into account
    void withdraw(int);     //withdrawals money from account
    int retdeposit() const; //function to return balance amount
    friend class BankingSystem;

}; //end of class Account

银行系统类:

class BankingSystem
{
    int accountID;
    char fileName;

private:
    std::vector<Account> accounts_;

public:
    static void addAccount();
    static void storeAccount( newAccount );
    void deleteAccount();
    void accountInquiry();
    void saveAccounts();
    void loadAccountsFromFile();
    friend class Account;

}; // end of class BankingSystem

我正在尝试以这种方式将新帐户存储在向量中。

1)BankingSystem.h中的addAccount函数

void BankingSystem::addAccount()
{
int ID;
float balance;
std::string pass, first, last;

cout << "\n\t Enter the Account ID: ";
cin >> ID;
cout << "\n\t Enter the passcode: ";
cin >> pass;
cout << "\n\t Enter Client's first name: ";
cin >> first;
cout << "\n\t Enter Client's last name: ";
cin >> last;
cout << "\n\t Enter starting balance: ";
cin >> setw(6) >> balance;

storeAccount( Account::createAccount( ID, balance, pass, first, last ) );

return;

}

2)在Account.h中创建Account

newAccount Account::createAccount( int ID, float balance, string first, string last, string pass )
{    
newAccount a;
a.accountID = ID;
a.accountBalance = balance;
a.firstName = first;
a.lastName = last;
a.accountPass = pass;

return a;

}

3)将帐户存储在Banking System.h中

void BankingSystem::storeAccount( newAccount a )
{
accounts_.push_back(a);

}

除了将数据存储在向量中之外,一切都工作正常。线路accounts_.push_back(a);有这个错误; “在静态成员函数中无效使用成员‘accounts_’。”


静态方法无权访问类实例(没有this)所以里面storeAccount and addAccount会员accounts_不存在。

仅供参考: return 语句之后不会执行任何内容,因此该行cout << "\n\t Account ID: " << a.accountID << " added successfully.";在您当前的代码中相当无用。

考虑以下实现以供参考:

using namespace std;

class Account
{
private: // data members
    string firstName;
    string lastName;
    string accountPass;
    int accountID;
    float accountBalance;

public:
    // constructor that initializes members
    Account(int id, float bal, const string& fname, const string& lname, const string& pass)
        : accountID(id), accountBalance(bal), firstName(fname), lastName(lname), accountPass(pass) {}

}; //end of class Account

class BankingSystem
{
private: // data members
    int accountID;
    char fileName;
    vector<Account> accounts_;

public:
    void addAccount()
    {
        int ID;
        float balance;
        string pass, first, last;

        // prompt input, initialize values, etc

            // construct a new Account from values and add it to vector
        accounts_.push_back(Account(ID, balance, first, last, pass));
    }
    void storeAccount( const Account& newAccount )
    {
            // add an already initialized account
        accounts_.push_back(newAccount);
    }


}; // end of class BankingSystem
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 将数据输入私有向量时出现问题(无效使用) 的相关文章

  • UTF8/UTF16 和 Base64 在编码方面有什么区别

    In c 我们可以使用下面的类来进行编码 System Text Encoding UTF8 System Text Encoding UTF16 System Text Encoding ASCII 为什么没有System Text En
  • 部署 MVC4 项目时出错:找不到文件或程序集

    过去 我只需使用 Visual Studio 2012 发布到 AWS 菜单项即可部署我的 MVC4 网站 到 AWS Elastic Beanstalk 现在 程序可以在本地编译并运行 但无法部署 从消息来看 它似乎正在寻找不在当前部署的
  • 自动从 C# 代码进行调试过程并读取寄存器值

    我正在寻找一种方法来读取某个地址的 edx 注册表 就像这个问题中所问的那样 读取eax寄存器 https stackoverflow com questions 16490906 read eax register 虽然我的解决方案需要用
  • 如何在 Unity 中从 RenderTexture 访问原始数据

    问题的简短版本 我正在尝试访问 Unity 中 RenderTexture 的内容 我一直在使用 Graphics Blit 使用自己的材质进行绘制 Graphics Blit null renderTexture material 我的材
  • SSH 主机密钥指纹与模式 C# WinSCP 不匹配

    我尝试通过 WinSCP 使用 C 连接到 FTPS 服务器 但收到此错误 SSH 主机密钥指纹 与模式不匹配 经过大量研究 我相信这与密钥的长度有关 当使用 服务器和协议信息 下的界面进行连接时 我从 WinSCP 获得的密钥是xx xx
  • C# 中值类型和引用类型有什么区别? [复制]

    这个问题在这里已经有答案了 我知道一些差异 值类型存储在堆栈上 而引用类型存储在托管堆上 值类型变量直接包含它们的值 而引用变量仅包含对托管堆上创建的对象位置的引用 我错过了任何其他区别吗 如果是的话 它们是什么 请阅读 堆栈是一个实现细节
  • 使用 C# 在 WinRT 中获取可用磁盘空间

    DllImport kernel32 dll SetLastError true static extern bool GetDiskFreeSpaceEx string lpDirectoryName out ulong lpFreeBy
  • 如何针对 Nancy 中的 Active Directory 进行身份验证?

    这是一篇过时的文章 但是http msdn microsoft com en us library ff650308 aspx paght000026 step3 http msdn microsoft com en us library
  • HttpClient 像浏览器一样请求

    当我通过 HttpClient 类调用网站 www livescore com 时 我总是收到错误 500 可能服务器阻止了来自 HttpClient 的请求 1 还有其他方法可以从网页获取html吗 2 如何设置标题来获取html内容 当
  • 为什么模板不能位于外部“C”块内?

    这是一个后续问题一个答案 https stackoverflow com questions 4866433 is it possible to typedef a pointer to extern c function type wit
  • 线程、进程和 Application.Exit()

    我的应用程序由主消息循环 GUI 和线程 Task Factory 组成 在线程中我调用一些第三方应用程序var p new Process 但是当我调用Application Exit 在消息循环中 我可以看到在线程中启动的进程仍在内存中
  • 我的 strlcpy 版本

    海湾合作委员会 4 4 4 c89 我的程序做了很多字符串处理 我不想使用 strncpy 因为它不会终止 我不能使用 strlcpy 因为它不可移植 只是几个问题 我怎样才能让我的函数正常运行 以确保它完全安全稳定 单元测试 这对于生产来
  • 像“1$”这样的位置参数如何与 printf() 一起使用?

    By man I find printf d width num and printf 2 1 d width num 是等价的 但在我看来 第二种风格应该与以下相同 printf d num width 然而通过测试似乎man是对的 为什
  • C 中的位移位

    如果与有符号整数对应的位模式右移 则 1 vacant bit will be filled by the sign bit 2 vacant bit will be filled by 0 3 The outcome is impleme
  • 可空属性与可空局部变量

    我对以下行为感到困惑Nullable types class TestClass public int value 0 TestClass test new TestClass Now Nullable GetUnderlyingType
  • 作为字符串的动态属性名称

    使用 DocumentDB 创建新文档时 我想设置属性名称动态地 目前我设置SomeProperty 像这样 await client CreateDocumentAsync dbs db colls x new SomeProperty
  • 如何构建印度尼西亚电话号码正则表达式

    这些是一些印度尼西亚的电话号码 08xxxxxxxxx 至少包含 11 个字符长度 08xxxxxxxxxxx 始终以 08 开头 我发现这个很有用 Regex regex new Regex 08 0 9 0 9 0 9 0 9 0 9
  • GDK3/GTK3窗口更新的精确定时

    我有一个使用 GTK 用 C 语言编写的应用程序 尽管该语言对于这个问题可能并不重要 这个应用程序有全屏gtk window与单个gtk drawing area 对于绘图区域 我已经通过注册了一个刻度回调gtk widget add ti
  • 窗体最大化时自动缩放子控件

    有没有办法在最大化屏幕或更改分辨率时使 Windows 窗体上的所有内容自动缩放 我发现手动缩放它是正确的 但是当切换分辨率时我每次都必须更改它 this AutoScaleDimensions new System Drawing Siz
  • C++ 成员函数中的“if (!this)”有多糟糕?

    如果我遇到旧代码if this return 在应用程序中 这种风险有多严重 它是一个危险的定时炸弹 需要立即在应用程序范围内进行搜索和销毁工作 还是更像是一种可以悄悄留在原处的代码气味 我不打算writing当然 执行此操作的代码 相反

随机推荐