I have two classes, let one be called List and the other Contact. The List class has a vector of Contacts that is private. I want to create a vector of pointers in my int main() that will point to select elements of the vector of Contacts inside List.
So far I have tried to create a pointer of type List and also in a seperate attempt a vector of pointers of class Contact, both which haven't worked. What else can I do?
List database;
List * buddies;
buddies->setContact(database.getContact(0), 0); //Assuming database.getContact(0), 0) exists,
//I want an element of the member contact of buddies to point to an element of the member of database
I also tried -
1 2 3
List database;
vector<Contact> * buddies;
buddies[0]=&database.getContact(0);
Thanks so much for the quick responses, sorry for including a lot of misc. code.
1. There is no getContact() method in the List class, so that won't work for sure. You probably need to call _contact(0), not getCnotact(0).
2. Your method _contact(int index) returns a copy of the contact stored in memory, which is no good. You need to return a reference if you want to store a pointer out of it.
3. You need to declare a vector<Contact*>, not a vector<Contact> *.
It has to be Contact& _rContact(int); for sure. But in main you have to buddies.push_back(&database._rContact(0));. See what's missing? The Address Of operator.