[Error] no match for 'operator>>'

This is my header file. Im trying to overload the istream operator and use ifstream in my main function to read in a text file with structured data (rows and columns). I am getting the error "[Error] no match for 'operator>>' (operand types are 'std::istringstream {aka std::basic_istringstream<char>}' and 'std::string {aka std::basic_string<char>}')
I commented where I am getting the error.
My main function is basically empty so far besides The class and object.
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

#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 
using namespace std;

class Record
{
	private:
			string name;
			int id;
			double rate;
			double hours;
	public: 
			Record();
			Record (string n, int empid, double hourlyRate, double hoursWorked); // constructor
			
			void read_data_from_file();
			double calculate_wage();
			void print_data();
			
			/* SETTERS AND GETTERS */			
			void set_name (string n);
			string get_name();
			
			void set_id (int empid);
			int get_id();
			
			void set_rate (double hourlyRate);
			double get_rate();
			
			void set_hoursWorked(double hoursWorked);
			double get_hoursWorked();
			/* END OF SETTERS AND GETTERS */
			
			friend istream& operator >> (istream& is, Record& employee)
			{
				string line;
				getline (is, line);
				
				istringstream iss(line);
				
				iss >> employee.get_name(); // where i get error
			}
						
};



This is my implementation/definition file

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
#include "Record.h"
Record::Record():name(), id(0), rate(0), hours(0) {} //default constructor must be implemented first
Record::Record(string n, int empid, double hourlyRate, double hoursWorked)
{
	name = n;
	empid = id;
	hourlyRate = rate;
	hoursWorked = hours;
}
//
void Record::set_name(string n)
{
	name = n;
}
string Record::get_name()
{
	return name;
}
//
void Record::set_id(int empid)
{
	id = empid;
}
int Record::get_id()
{
	return id;
}
//
void Record::set_rate(double hourlyRate)
{
	rate = hourlyRate;
}
double Record::get_rate()
{
	return rate;
}
//
void Record::set_hoursWorked(double hoursWorked)
{
	hours = hoursWorked;
}
double Record::get_hoursWorked()
{
	return hours;
}
//


void Record::read_data_from_file()
{
	
}
double Record::calculate_wage()
{
	return (rate * hours);
}
Because i don't see the overloading the operator >> for the type istringstream. The compiler can't found the correct overloading for the operator >>.

I think is this the problem.
Hello tidematic,

Take a close look at what you are trying to do. iss >> employee.get_name();. You are reading from "iss" into a function that returns "name" from the class's private variables. This is not going to work.

I have no idea what "line" or "iss" contains at this point which means I can only guess at how to read from "iss" for what you need. My first thought would be std::getline(iss, name);, but "getline" may nee a third parameter to work properly. It would depend on what "line" contains.

The first bit of code should be in a header file and the "#include"s should not be there and especially the using namespace std;. Read this for an explanation:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

The second bit off code should be a ".cpp" file where the "#include"s from the first bit need to be here above line 1.

Give me an example of what "line" i line 40, header file, looks like and I can give you a better idea of what line 44 should be.

Hope that helps,

Andy
Topic archived. No new replies allowed.