no operator "<<" matches these operands
Aug 12, 2017 at 4:15pm UTC
hello,
when i run this code , i am getting this error:
no operator "<<" matches these operands at line 39:
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
# include <iostream>
# include <algorithm>
# include <vector>;
using namespace std;
struct studentinfo {
string name;
int grade;
};
bool name(const studentinfo & lhs, const studentinfo & rhs)
{
return lhs.name < rhs.name;
}
int main()
{
vector <studentinfo> s;
studentinfo sinfo;
sinfo.name = "gg" ;
sinfo.grade = 44;
studentinfo sinfo2;
sinfo.name = "gga" ;
sinfo.grade = 44;
s.push_back(sinfo);
s.push_back(sinfo2);
sort(s.begin(), s.end(), name);
for (auto x : s)
{
cout << x.name << x.grade << endl;
}
}
Aug 12, 2017 at 4:23pm UTC
#include <string>
Aug 12, 2017 at 6:14pm UTC
Why does does the compiler know string but not << for string without including <string> ?
Aug 12, 2017 at 6:40pm UTC
That depends on implementation. The <string> could be a mere wrapper file that includes "real headers" and the other wrappers might include (some) of those same "real files".
The standard says what headers we should include. We should not depend on undocumented implementation-specific dependencies.
Aug 13, 2017 at 7:10pm UTC
JLBorges is right.
Put #include <string> at the beggining of your code and the problem will be solved. :)
Have a nice day, my friend! :)
Topic archived. No new replies allowed.