Help with pointers and vectors

Hello everyone!

I'm new and I'm having problems with my code, I need to have a vector of "Accounts" and a vector of "Customers", I need to be able to create a new account that can point to a customer and retrieve information from it.

So far my feeble attempts have got me this;

1
2
3
4
5
6
7
8
9
10
11
12
13
			for (int i = 0; i < customer.size(); i++)
			{
				if (name == customer[i] -> getName())
				{
					
					customer[i] = new Account();
					Account stdAccount(balance, accountType, accountNumber, owner);
					
					account.push_back(stdAccount);
					cout << "Account Created\n";
					cout << account[i] -> getOwner();
				}
			}


Its all over the place as I really don't know what I'm doing. I'm trying to go through the vector, see if the name that was entered matches any of the names stored in each vector element, and then try and create an account that references the customer it belongs to.

Any advice? Sorry for being incredably indescriptive.

Thanks
Is 'customer' a vector<Customers>, a vector<Accounts>, a vector<Customers*> or a vector<Accounts*>?

Naming-wise, I'd think the 1st or 3rd option, but line 6 suggests the 4th. new returns a pointer to the created object, not an object.

[edit]

The reason I'm not sure is because you don't seem to be sure. On line 9, you're pushing an Account object on 'account', but on line 11 you're trying to access one of its member functions as if it were a pointer.
Last edited on
Once again sorry for the horrible description, I'll try better now.

"customer" is a vector<Customer>
"account" is a vector<Account>

I was trying to use vector<Customer*> and vector<Account*> in the code above, but I'm not sure if thats right.

At the moment, I'm using those vectors to store all the customers and accounts that are created. (not pointers, just the actual accounts and customers) The code your seeing is from the Main class.

I need to somehow go through all the customers (The for loop).
Find out if the name entered matchs any of the customers names.
Create an account stored in the vector<Account> that *knows* what customer it belongs to.

Eventually I want to be able to query the Account and ask who it belongs to and have that pronted to the console.

I hope that makes things a bit clearer.

Thanks again.
a) If customer is a vector<Customer>, why line 6? That's not just a different object type (Account, not Customer) but also a Pointer to an object rather than an object!

b) If customer is a vector<Customer>, customer[i] is a Customer object [provided i is within the bounds of customer, but your for loop has that covered, so no worries there]. That means accessing a member is done using the dot (.) operator, not the arrow (->) operator. Remember that pObject->member basically means (*pObject).member, i.e. a dereferencing (*) and dot operator in one.

Thanks for pointing that out, I've removed line 6 and changed line 11.

Could you explain how I could create an account stored in the vector<Account> that *knows* what customer it belongs to?

I just don't really have a good place to start (hence the random code snippets above that didn't make much sense) as I'm new to C++ and pointers in general.

Thanks again.
There's many ways to do it, but it depends on the situation...

How many accounts can a customer have? How many customers can an account belong to? Do you need to access accounts through the user (i.e. given a user, find his account(s)), or the other way (i.e. given an account, find the user(s) it belongs to), or both?

If each user has one account, you could ensure 'forward access' by giving the Customer class an Account member, or an Account pointer member that points to an account in accounts[], or simply an integer 'accountID' that stores in the index of that Customer's account in accounts[]. If you need 'backward access', you can use those two last options in the reverse way as well.

Alternatively, if a user and account are *always* attached to each other, you could simply pair them up:
1
2
3
4
struct CAPair {
   Account acc;
   Customer cust;
}

Then, keep vector of CAPairs.

It all depends on how you organize your data, which depends on the functionality you need. If you have the time, it might be fun/interesting to try several methods to see the (dis)advantages of each.

The customer can have *In theory* as many accounts as he/she wants. The customer should be able to find there accounts (given the customer, find the accounts they have) and vise versa.

I'd idealy like to achieve this using pointers, could I create an account, store it in the vector of Accounts, and pass a pointer to a customer in the Customer vector into the constructor of the account?

If so, how could I then use the pointer to display the accounts owner?

If you look at the code, the last parameter in the account creation line, has "owner" in it, this is a pointer that I'm hoping can point to the customer it belongs to. How could I then (In the Account class) use that pointer that was passed through to get it's owners information?

Hope this makes sense
Topic archived. No new replies allowed.