Vector of objects

Hi all,

I am on the verge of finishing my banking application, although I have hit an issue whilst creating a vector of objects.

From what I understand/have read, you initialise your vector as follows:

1
2
#include <vector>
vector <Class> vectorname;


in my case...

vector <Customer> Customers;

I then thought to add an element to the back of the vector I would use:

Customers.push_back(new Customer()); // calls Customer constructor with no args and adds to back of vector.

Although I get the following error:
4 IntelliSense: no instance of overloaded function "std::vector<_Ty, _Ax>::push_back [with _Ty=Customer, _Ax=std::allocator<Customer>]" matches the argument list d:\bank\bank\test.cpp 39


Any thoughts?

Cheers

Matt
Last edited on
Had to remove new from customer constructor

Thanks anyway
I haven't been programming in C++ for a while, so I don't really remember if C++ has anonymous objects or if that's a Java thing. That may be one of the problems.

The other problem is your vector type is Customer not pointers to a Customer. The new keyword allocates memory in the heap, and in C++ you would need a pointer to refer to memory addresses. So if you want to do that you'll probably need a vector of Customer pointers. That may be what is going wrong there.
Last edited on
Topic archived. No new replies allowed.