【问题标题】:I cant use the global class vector我不能使用全局类向量
【发布时间】:2021-09-21 04:17:06
【问题描述】:
#include <iostream>
using namespace std;
#include<vector>
#include "CustomerAccount.h"



vector<CustomerAccount>system;

void CreateAccount(vector<CustomerAccount>&system) {
    string name;
    string email;
    int pass;
    int phone;
    cout << "Pease fill your information : " << endl;
    cout << "Please enter your name : ";
    cin >> name;
    cout << "Please enter your Email : ";
    cin >> email;
    cout << "Please enter the Password : ";
    cin >> pass;
    cout << "Please enter your phone number : ";
    cin >> phone;
   
    system.push_back(CustomerAccount(name, email, pass, phone));

}

void customer() {
    cout << "Choose one : " << endl;
    cout << " 1)Login to an existing account " << endl;
    cout << " 2)Create a new customer account  " << endl;
    int MainNum{ 0 };
    cin >> MainNum;
    if (MainNum == 1)
        cout << "k";
    else if (MainNum == 2)
        CreateAccount(system);
        
  



}

   This is normal text        

嗨, 我想做一个小型餐厅系统程序,程序会询问用户他是管理员还是顾客。 如果他是客户,程序会询问用户是否要创建新帐户。 所以我添加了客户账户类,然后我制作了一个客户账户类的向量(系统),并将向量设为全局。 Create Account 函数将在每次调用该函数时将一个新对象推回 Customer 帐户类。 问题是当我使用系统向量作为函数创建帐户的参数时出现错误,我不知道为什么?

感谢您阅读我的问题

【问题讨论】:

  • system 完全能够访问这个全局声明的向量时,为什么还需要将它传递给CreateAccount
  • 这就是重点,如果没有通过就会出错

标签: c++ function class vector global


【解决方案1】:

我不建议使用全局变量,因为它在实践中是糟糕的设计。但是,为了回答您的问题,并在没有看到您的标头声明的情况下制作基线 CustomerAccount 类...

您的示例对全局变量名称使用了模棱两可的声明。此外,它也是执行系统调用的原生函数,所以我建议使用其他的。

除此之外,以下内容应该对您有所帮助:

#include <iostream>
#include<vector>

using namespace std;

class CustomerAccount
{
public:
    CustomerAccount(string name, string email, int pass, int phone);

private:
    string mName;
    string mEmail;
    int mPass;
    int mPhone;
};

CustomerAccount::CustomerAccount(string name, string email, int pass, int phone) 
    : mName(name), mEmail(email), mPass(pass), mPhone(phone)
{
  
}

vector<CustomerAccount> g_system;

void CreateAccount(vector<CustomerAccount>& system) {
    string name;
    string email;
    int pass;
    int phone;
    cout << "Pease fill your information : " << endl;
    cout << "Please enter your name : ";
    cin >> name;
    cout << "Please enter your Email : ";
    cin >> email;
    cout << "Please enter the Password : ";
    cin >> pass;
    cout << "Please enter your phone number : ";
    cin >> phone;

    system.push_back(CustomerAccount(name, email, pass, phone));
}

void customer() {
    cout << "Choose one : " << endl;
    cout << " 1)Login to an existing account " << endl;
    cout << " 2)Create a new customer account  " << endl;
    int MainNum{ 0 };
    cin >> MainNum;
    if (MainNum == 1)
        cout << "k";
    else if (MainNum == 2)
        CreateAccount(g_system);
}

void main()
{
    customer();
}

更新: 要在不使用全局变量的情况下获得相同的结果,请将类中包含的向量作为成员变量:

#include <iostream>
#include<vector>

using namespace std;

struct AccountInfo
{
    string name;
    string email;
    int pass;
    int phone;
};

class CustomerAccounts
{
public:
    CustomerAccounts();
    void CreateNewAccount();

private:
    std::vector<AccountInfo> mAccounts;
};

CustomerAccounts::CustomerAccounts()
{
}


void CustomerAccounts::CreateNewAccount()
{
    AccountInfo info;

    cout << "Pease fill your information : " << endl;
    cout << "Please enter your name : ";
    cin >> info.name;
    cout << "Please enter your Email : ";
    cin >> info.email;
    cout << "Please enter the Password : ";
    cin >> info.pass;
    cout << "Please enter your phone number : ";
    cin >> info.phone;
    
    mAccounts.push_back(info);
}

int main()
{
    CustomerAccounts accounts;

    cout << "Choose one : " << endl;
    cout << " 1)Login to an existing account " << endl;
    cout << " 2)Create a new customer account  " << endl;
    int MainNum{ 0 };
    cin >> MainNum;
    if (MainNum == 1)
        cout << "k";
    else if (MainNum == 2)
        accounts.CreateNewAccount();

    return 0;
}

【讨论】:

  • 谢谢,但我想问你是否建议没有全局向量的另一种方法,如果你能解释为什么当你刚刚将向量的名称更改为 g_system 时错误消失了?
  • @AymanFreihat 如果这有助于解决您的问题,请将答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 2012-04-06
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多