cin to vector with class member

I get some errors on this code but i don´t know why.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Phonebook

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class Phonebook
{
public:
	Phonebook() : name(""), number(0) {}
	Phonebook(const string &_name, int _number) : name(_name), number(_number) {}
	Phonebook(const Phonebook &rhs) : name(rhs.name), number(rhs.number) {}
	Phonebook operator=(const Phonebook &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		number = rhs.number;
		return *this;
		
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _number)
	{
		name = _name;
		number = _number;
	}
	
	const string &Name() const { return name; }
	int Number() const { return number; }

	private:
	string name;
	int number;
};

//Function Declarations
void fillPhonebook(vector<Phonebook>&myPhonebook);
//fillVector
//@param vector<Class>)& - the vector to be filled

int i = 0;
int deleteFromVector;

int main()
{
	vector<Phonebook>myPhonebook;
	
	while(true)
	{
		cout << " - Press 1 to add a person and number" << endl;
		cout << " - Press 2 to print all persons and numbers" << endl;
		cout << " - Press 3 to delete person and number" << endl;
		cout << " - Press 4 to quit" << endl;

		int choice;
		cin >> choice;
	
		switch(choice)
		{

		case 1:
		{
			fillPhonebook(myPhonebook);
		}
		break;		

		case 2:
		{

			cout << "Print all names and numbers" << endl;
					
		}
		break;
		case 3:
		{
			cout << "Which index do you want to delete? (1-10)" << endl;
      
		}

		}
		break;
	
	}

	return 0;
}

// Function Definitions
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
			string name;
			int number;		

				// Flush makes sure that the buffer is cleared and the characters are written to their destination
			cout << "Enter persons name: " << flush << endl;
		
			// Enter values into vector
			cin >> name;
			
			cout << "Enter persons number: " << flush << endl;
		
			// Enter values into vector
			cin >> number;
			myPhonebook.push_back(name, number);

}
error on line 112:38: error: no matching function for call to ‘std::vector<Phonebook>::push_back(std::string&, int&)’
myPhonebook.push_back(name, number);


Wrong usage of push_back()
Prototype : void push_back (const value_type& val);
http://www.cplusplus.com/reference/vector/vector/push_back/


You need to do something like this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
			string name;
			int number;		

				// Flush makes sure that the buffer is cleared and the characters are written to their destination
			cout << "Enter persons name: " << flush << endl;
		
			// Enter values into vector
			cin >> name;
			
			cout << "Enter persons number: " << flush << endl;
		
			// Enter values into vector
			cin >> number;
                        Phonebook person(name, number); // Dis
			myPhonebook.push_back(person);

} 
Alternatives:
1
2
3
myPhonebook.push_back(Phonebook(name, number));
myPhonebook.push_back({name, number});
myPhonebook.emplace_back(name, number);


Also you do not need lines 18-21 in assigment operator. It handles self-assigment gracefully even without them and comparsion uses resources too
Last edited on
Altered, and looked throug some old code, at least on the way but need some help still.

myPhonebook.push_back(Phonebook(name, number));
Got it to work!!! Thanks MuNiPaa!!!!!

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Phonebook

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class Phonebook
{
public:
	Phonebook() : name(""), number(0) {}
	Phonebook(const string &_name, int _number) : name(_name), number(_number) {}
	Phonebook(const Phonebook &rhs) : name(rhs.name), number(rhs.number) {}
	Phonebook operator=(const Phonebook &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		number = rhs.number;
		return *this;
		
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _number)
	{
		name = _name;
		number = _number;
	}
	
	const string &Name() const { return name; }
	int Number() const { return number; }

	private:
	string name;
	int number;
};

//Function Declarations
void fillPhonebook(vector<Phonebook>&myPhonebook);
//fillVector
//@param vector<Class>)& - the vector to be filled

void printPhonebook(vector<Phonebook>&myPhonebook);
//printVector - prints the information of all students
//@param const vector<Student>& - students in class 

int i = 0;
int deleteFromVector;

int main()
{
	vector<Phonebook>myPhonebook;
	
	while(true)
	{
		cout << " - Press 1 to add a person and number" << endl;
		cout << " - Press 2 to print all persons and numbers" << endl;
		cout << " - Press 3 to delete person and number" << endl;
		cout << " - Press 4 to quit" << endl;

		int choice;
		cin >> choice;
	
		switch(choice)
		{

			case 1:
			{
				fillPhonebook(myPhonebook);
			}
		
			case 2:
			{

				cout << "Print all names and numbers" << endl;
				printPhonebook(myPhonebook);
					
			}
		
			case 3:
			{
				cout << "Which index do you want to delete? (1-10)" << endl;
      
			}
			case 4:
				break;
		}
	
	}

	return 0;
}

// Function Definitions
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
		string name;
		int number;					

		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << "Enter persons name: " << flush << endl;
		
		// Enter values into vector
		cin >> name;
			
		cout << "Enter persons number: " << flush << endl;
		
		// Enter values into vector
		cin >> number;

		Phonebook Phonebook(name, number);
		myPhonebook.push_back(Phonebook);

			/*myPhonebook.push_back(Phonebook(name, number));*/
			
}

void printPhonebook(vector<Phonebook>&myPhonebook)
{
	
	for (int i = 0; i < myPhonebook.size(); i++)
	{	
		cout << "Name: " << myPhonebook[i].Name() << endl;
		cout << "Number: " << myPhonebook[i].Number() << endl;
		cout << endl;
	}
}
Topic archived. No new replies allowed.