Storing Array into Vector Class

Hello!

I need some help writing a class that allows me to store an array into a class that uses Vectors as the underlying structure. I am having trouble in storing the array into the private member vector.

Can anyone help me figure this out?

I know i need to create a constructor that has can store the information from another class into this vector. But in main I create the array.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int const MAX=2;

class Contact
{
	private:
		string first;
		string last;
		
	public:
		//contructor
		void setnames(string f, string l)
		{
			cout << "type names: ";
			
			getline(cin,f);
			getline(cin,l);
			
			first = f;
			last = l;
		}
		//acessor
		string getfirst()
		{
			return first;
		}
		string getlast()
		{
			return last;
		}
		
};

class ContactList
{
	private:
		vector <Contact> info;
		
	public:
		//constructor that creates an empty contact list
		ContactList();
		//constructor that uses array of Contact
		void setVecNames(Contact &array)
		{
			info(array);
		}
		
};
ContactList::ContactList()
{
	first = "Empty";
	list = "Empty";
}


int main()
{
	Contact name[MAX];
	ContactList work;
	string f, l;

        //Creating first and last name then printing it
	for(int i=0; i<MAX; i++){
		name[i].setnames(f,l);
		cout << name[i].getfirst() << name[i].getlast() << endl;
	}

	//sending array called 'name' to vector class 'Contactlist'
	work.setVecNames(name);
	
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//contructor -> actually a normal member function
void setnames(string f, string l)
{
    cout << "type names: ";
    
getline(cin,f);
    getline(cin,l);
    
    first = f;
    last = l;

    getline(cin,first);
    getline(cin,last);
}


1
2
3
4
5
6
7
8
9
// contact list has no member first or list
ContactList::ContactList()
{
	first = "Empty";
	list = "Empty";
}

// cant convert array to reference
work.setVecNames(name);

I'm not sure how this compiled.

1
2
3
4
5
void setVecNames(Contact array[MAX])
{
    // loop through array
    // use push_back to assign elements
}
Topic archived. No new replies allowed.