Pointers and Classes

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?
Elaborate on what exactly you did. No better yet, just show us the code you used for the list pointer, and tell us exactly what hasn't worked.
I don't see much of a problem, unless I'm misunderstanding:

-List has a private std::vector<Contact>.
-You want in main() a std::vector<Contact*> that points to individuals inside the List instance.

Just make sure your List can hand out the addresses of the contacts it stores and that's it! I'd probably just overload operator[]:

Contact& operator[](int index) { return _v[index]; }
Sorry bout that, here's my code:

Contact.h -
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
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>

using namespace std;

#ifndef H1HEADER_H
#define	H1HEADER_H

class Contact {
protected:
    string name, email, phone, ptype, address, atype, website, birthday, notes;
public:
    //Constructor
    Contact();
    
    //Getters
    string getName();
    string getEmail();
    string getPhone();
    string getPtype();
    string getAtype();
    string getAddress();
    string getWebsite();
    string getBirthday();
    string getNotes();
    
    //Setters
    void setName(string);
    void setEmail(string);
    void setPhone(string);
    bool setPtype(char);
    bool setAtype(char);
    void setAddress(string);
    void setWebsite(string);
    void setBirthday(string);
    void setNotes(string);
    
    //Miscellaneous
    void print();
};

#endif	/* H1HEADER_H */ 

List.h -
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
30
31
#include<iostream>
#include<vector>
#include"contact.h"

using namespace std;

#ifndef LIST_H
#define	LIST_H

class List{
    vector<Contact> contact;
    bool checkLeapYear(int);
    //Verification
    bool verifyName(string);
    bool verifyEmail(string);
    bool verifyPhone(string);
    bool verifyWebsite(string);
    bool verifyBirthday(string);
public:
    Contact _contact(int);
    vector<Contact> getContacts();
    void setContact(Contact, int);
    void print();
    void add();
    void remove(int);
    int find();
    int search(string);
    int size();
};

#endif	/* LIST_H */ 


Important Functions of List:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Contact List::_contact(int index){
    return this->contact[index];
} 
vector<Contact> List::getContacts(){
    return contact;
}
void List::setContact(Contact temp, int index){
    contact[index].setName(temp.getName());
    contact[index].setEmail(temp.getEmail());
    contact[index].setPhone(temp.getPhone());
    contact[index].setAddress(temp.getAddress());
    contact[index].setWebsite(temp.getWebsite());
    contact[index].setBirthday(temp.getBirthday());
    contact[index].setNotes(temp.getNotes());
}

In int main() -
1
2
3
4
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.
You need to pass and return the Contact objects by reference (Contact &) or you'll just copy the contact information into a new list.
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> *.
Ok so I'm going with the second method here:

1
2
3
List database;
vector<Contact*> buddies;
buddies.push_back(database._rContact(0));

and I get a no matching function for call error.

buddies->push_back(database._rContact(0));

also doesn't work.

where
1
2
3
4
5
6
Contact & _rContact(int); // In header file

Contact & List::_rContact(int index){ // In source file
    Contact & temp=contact[index];
    return temp;
} 


EDIT:
How would I go about doing it using the first method, that is List * buddies?
Last edited on
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.
Topic archived. No new replies allowed.