Vector as member in a class used for a function

Hi i am having trouble with my program. i have a class called customer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Customer
{
    private:
        string username;
        string password;
        vector<Account*> accNum;
        string name;
        string address;
        int phone;
    public:
        Customer(string inUser = "Default", string inPass = "12345", string inName = "Default", string inAddress = "Default", int inPhone = 0000000);
        void setPass(string inPass);
        void setUser(string inUser);
        bool checkUser(string inUser);
        bool checkPass(string inPass);
        void setName(string inName);
        void setAddress(string inAddress);
        void setPhone(int inPhone);
        const string getUser();
        const string getPass();
        const string getName();
        const string getAddress();
        const int getPhone();
        void addAccount(Account* inAccount);
        void display();
        void viewAccount();
        vector<Account*> getAccount();
        Account* getAccNum(int inNum);
};


and then i have an abstract class called Account. So when i try to create a new account using the vector of the customer class and store it in the vector by using a function.
1
2
3
4
void storeAccount(vector<Account*> inVector, Account* inAccount)
{
    inVector.push_back(inAccount);
}


but when ever i exit out of this function the vector is empty. i have been looking around for a solution and i cant seem to solve it please help.
Your member data of type vector has name

vector<Account*> accNum;

In our function storeAccount you fill parameter

vector<Account*> inVector

that is a local object for the function and will be deleted after exiting from it.

1
2
3
4
void storeAccount(vector<Account*> inVector, Account* inAccount)
{
    inVector.push_back(inAccount);
}
what is the best way to solve this problem so that each customer can have one to many accounts using vectors. can i declare a vector on the main loop and have like

vector<Accout*> someName;

take this and use it for the class Customer? Any other way to solve it so that a Customer object has a collection of Account* Object

please im new at this stuff and would really appreciate for your help.
At least to use your class member

1
2
3
4
void storeAccount( Account* inAccount)
{
    accNum.push_back(inAccount);
}


provided that this function is a member function of Customer.
Last edited on
Since storeAccount is not a member function in the above code, the simplest fix is to change the argument inVector to a reference. Then the vector will be modifed in the calling code when the function is complete:

1
2
3
4
void storeAccount(vector<Account*>& inVector, Account* inAccount)
{
    inVector.push_back(inAccount);
}


Topic archived. No new replies allowed.