Help with vectors!

So i'm currently making a code that is supposed to carry employee data based on the users input. The user inputs data and then a new object is created that will carry it later. But i have some questions.

1. When outputting the vector (employees) i get this error.
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and '__gnu_cxx::__alloc_traits >::value_type {aka Employee}')
Any idea why?

2. Have i done correct in creating a new object everytime the user presses 1 and then inputs the info. My idea is to after i've created the object in the vector apply it.

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
  #include <stdio.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Employee
{
 public:
 
 int addvector;
 
 string name;
 int age;
 string id;
 double wage;

    
};

int main()
{

vector <Employee> employees;
Employee emp;
emp.addvector = 1;

int userinput;

string employeeName;
int employeeAge;
string employeeId;
string employeeWage;


cout << "Press 1 to add employee, Press 2 to view employee, Press 0 to exit." << endl;
cin >> userinput;

if (userinput == 0)
{
    return 0;
}
else if (userinput == 1)
{
    
    cout << "Input employee name." << endl;
    cin >> employeeName;
    
    cout << "Input employee age." << endl;
    cin >> employeeAge;
    
    cout << "Input employee ID." << endl;
    cin >> employeeId;
    
    cout << "Input employee wage." << endl;
    cin >> employeeWage;
    
    employees.push_back(Employee(emp));
    
    cout << employees[0];
    
}


}



you either need to overload a << operator for cout to use for the employee class or rewrite your code to feed cout something it can handle, such as
cout << employees[0].name;

The code looks ok as far as it goes. I would think since employees is a vector you would loop over input until the user enters a zero to quit.

Its <cstdio> not stdio.h, to use the c++ version of the header (minor but can cause issues in large projects).

Its a little weird to just quit without any warns if neither 0 nor 1 are entered. But that looks like a 'not done yet' problem, more than a real problem.
Last edited on
Thanks for the help it worked but i've gotten into a new problem and i get this error when trying to run it.

error: no match for 'operator<<' (operand types are 'std::basic_ostream' and '__gnu_cxx::__alloc_traits >::value_type {aka Employee}')

cout << "Name: " << employees[employeeCode].name << endl << "Age: " << employees[employeeCode].age << endl << "ID: " << employees[employeeCode].id << endl << "Wage: " << employees[employeeCode] << endl;

and

error: cannot bind 'std::basic_ostream' lvalue to 'std::basic_ostream&&'
cout << "Name: " << employees[employeeCode].name << endl << "Age: " << employees[employeeCode].age << endl << "ID: " << employees[employeeCode].id << endl << "Wage: " << employees[employeeCode] << endl;

Any ideas?

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
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Employee
{
 public:
 
 int addvector;
 
 string name;
 int age;
 string id;
 double wage;

    
};

int main()
{

while(1) {

vector <Employee> employees;
Employee emp;
emp.addvector = 1;

int userinput;
int employeeCount = 0;
int employeeCode;

string employeeName;
int employeeAge;
string employeeId;
double employeeWage;


cout << "Press 1 to add employee, Press 2 to view employee, Press 0 to exit." << endl;
cin >> userinput;

if (userinput == 0)
{
    return 0;
}
else if (userinput == 1)
{
    
    
    cout << "Input employee name." << endl;
    cin >> employeeName;
    
    cout << "Input employee age." << endl;
    cin >> employeeAge;
    
    cout << "Input employee ID." << endl;
    cin >> employeeId;
    
    cout << "Input employee wage." << endl;
    cin >> employeeWage;
    
    employeeCount++;
    
    employees.push_back(Employee(emp));
    
    employees[employeeCount].age = employeeAge;
    employees[employeeCount].name = employeeName;
    employees[employeeCount].id = employeeId;
    employees[employeeCount].wage = employeeWage;
    
    cout << "Employee has been added with employee code: " << employees.size() << endl;
    
}

else  if (userinput == 2)
{
    
    cout << "Please input employee code." << endl;
    cin >> employeeCode;
    
    cout << "Name: " << employees[employeeCode].Name << endl << "Age: " << employees[employeeCode].Age << endl << "ID: " << employees[employeeCode].id << endl << "Wage: " << employees[employeeCode] << endl;
    
}

}

}




error: no match for 'operator<<' (operand types are 'std::basic_ostream' and '__gnu_cxx::__alloc_traits >::value_type {aka Employee}')
This is saying it doesn't know how to handle an operation with the << operator between your cout and your Employee, e.g. cout << Employee. (cout is an std::basic_ostream).

Look at your line 83, specifically the end of it. It looks like you're trying to print out the wage. You forgot to add the ".wage" to the end.
Thanks but now it's giving me "Segmentation fault" after entering the information or writing the employeecode. Help?
You re-create your vector every loop iteration because all of your logic is surrounding by your while loop. Is this intended?

Also, let's step through your logic here. employeeCount is initially 0. The size of your vector is also initially 0. (In fact, your use of the employeeCount variable is redundant because std::vectors keep track of their own size(), employees.size(). ) Then, you push_back an Employee, and increment Employee count by 1. But array/vector indices start at 0, not 1. You are trying to access employees[1], but the first element of employees is at index 0. Try changing employees[employeeCount] to employees[employeesCount-1], or equivalently, just do employees.back().

1
2
3
4
    employees.back().age = employeeAge;
    employees.back().name = employeeName;
    employees.back().id = employeeId;
    employees.back().wage = employeeWage;

Last edited on
Topic archived. No new replies allowed.