Reputation:
I have the operator in my header as such:
friend std::istream& operator>> ( std::istream& is, StudentRecord& sr );
and in my class:
std::istream& operator>>(std::istream& is , StudentRecord& sr){
is >> sr.name >>std::ws>> sr.surname>>std::ws>>sr.studentNumber>>std::ws;
getline(is, sr.classRecord);
return is;
}
However, I get the following error:
std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&
in a database class:
void Database::read(string f)
{
studRcrds.clear();
std::ifstream in(f.c_str());
string studLine;
while (!(in.eof()))
{
getline(in,studLine);
std::istringstream sin(studLine);
StudentRecord newStudent();
sin >> newStudent;
}
}
Upvotes: 0
Views: 5820
Reputation: 1
You may also change your getline function:
istream& getline(std::istream& is, StudentRecord& sr)
without using a newStudent object.
Upvotes: -2
Reputation: 171443
The error is a bit confusing, but it means there is no operator>>
that can be used for sin >> newStudent;
That is because you have declared newStudent
as a function (see most vexing parse). The wording of the error is because there is an overload for rvalue streams that accepts anything on the right-hand side:
template<typename CharT, typename TraitsT, typename T>
basic_istream<CharT, TraitsT>&
operator>>(basic_istream<CharT, TraitsT>&& istr, T&&);
and because there is no matching operator for the function type of newStudent
it tries to call that overload, which can't bind an rvalue-reference to sin
.
The fix is to avoid the most vexing parse:
StudentRecord newStudent;
or:
StudentRecord newStudent{};
or:
StudentRecord newStudent = newStudent();
or similar. With that change newStudent
has the right type and so can use your operator>>
overload.
Upvotes: 9