Error filling a vector using struct

I created a struct called employee and when I compile, I get an error telling me this:employees.cpp:44:25: error: no matching function for call to 'getline(std::ifstream&, employee&)'............What's up? I thought it looked good, but I'm pretty new so obviously it isnt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void fill_vector( vector<employee> &employees){

        ifstream input;
        input.open("employees.txt");
        getline(input, s);
        while(!input.eof()){
                istringstream instr(s);
                while(!instr.eof()){
                        instr >> s.last >> s.first >> s.salary;
                        employees.push_back(s);
                }
                getline(input, s);
        }
        input.close();
}
getline only works with strings
You didn't declare 's'. add string s at the beginning of your function.

Edit: I think I see what you're trying to do, and why you're getting the error.
You're trying to read in a line of text with employee information and parse it into your employee struct. I assume you have a struct called employee somewhere.

in your innermost while loop you have a variable called s which your compiler assumes employee struct. But you've never declared it as such. You've just tried to set members of s. It looks like you're using s to refer to a string when you call getline(), but you also use it to refer to an employee struct in your innermost while loop.

You should declare a separate employee variable in your innermost while loop.
By the way, your code won't work properly as written, since you need to dynamically allocate your employee structs. Otherwise, they will get destroyed at the end of the while loop.
Last edited on
What can I use if I want to read in my struct? It has 3 attributes: string last, string first and double salary............I want to read the file 1 line at a time and used this method with strings before using getline..........is there an equivalent for structures?
Sorry, I was confused about the dynamic allocation. your code should work fine if you declare variables for the string and employee, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void fill_vector( vector<employee> &employees){
        string s;
        ifstream input;
        input.open("employees.txt");
        getline(input, s);
        while(!input.eof()){
                istringstream instr(s);
                while(!instr.eof()){
                        employee e;
                        instr >> e.last >> e.first >> e.salary;
                        employees.push_back(e);
                }
                getline(input, s);
        }
        input.close();
}
Ya just figured it out.........................I have another problem....................how do I print out elements of a vector that i filled with a struct containing the same three attributes, using a pointer? I keep getting errors telling me that there is no operator << when I deference the pointer.
an iterator would be the correct way but you can also use the array style []

example
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct employee
{
	string firstname;
	string lastname;
};

int main()
{
	vector<employee> myEmployee;
	employee tempEmployee;
	string tempString;
	for (size_t i = 0; i < 3; i++)
	{
		cout << "Please enter the enployee's frist name: ";
		getline(cin, tempString);
		cout << endl;
		
		tempEmployee.firstname = tempString;

		cout << "Please enter the enployee's second name: ";
		getline(cin, tempString);
		cout << endl;
		tempEmployee.lastname = tempString;

		myEmployee.push_back(tempEmployee);
	}
	

	cout << "Output using iterator" << endl;
	for (auto it = myEmployee.begin(); it != myEmployee.end(); it++)
	{
		cout << "firstname = " << it->firstname << " lastname = " << it->lastname << endl;
	}

	cout << "Output using []" << endl;
	for (size_t i = 0; i < 3; i++)
	{
		cout << "firstname = " << myEmployee[i].firstname << "Lastname = " << myEmployee[i].lastname << endl;
	}



	cin.ignore();
	return 0;
}
how do I print out elements of a vector that i filled with a struct containing the same three attributes, using a pointer? I keep getting errors telling me that there is no operator << when I deference the pointer.

We should not guess what you do. You should show what you do.

There is no overload of getline() or operator<< for struct employee, but you can change that by defining such overloads. You will then get a simpler main().
Topic archived. No new replies allowed.