no operator '>>' matches these operands
my program wont compile because it did not found match for the operand. It accesses the map in struct Student, I am not sure if this is the exact way to access map.
my program wont compile because it did not found match for the operand. It accesses the map in struct Student, I am not sure if this is the exact way to access map.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
#include <list>
using namespace std;
struct Student {
string id;
map<string, int> scores;
};
istream& operator >>(istream &is, Sudent& g) {
auto it = g.scores.begin();
is >> g.id >> it->first >> it.second;
return is;
}
On >> it->first
I get this error:
Error: no operator ">>" matches these operands
operand types are: std::basic_istream<char, std::char_traits<char>> >> const std::string
你可以使用临时变量
std::string tempStr;
int tempInt;
is >> g.id >> tempStr >> tempInt;
scores.insert( std::pair<std::string,int>(tempStr , tempInt));
The error is due to the fact that it->first
is of type const string
, not string
.
Apart from this, you need to find a way to read that map by reading an (unknown) number of string(s) and the corresponding int. How to do that depends on how they are stored in the file.
链接地址: http://www.djcxy.com/p/86306.html下一篇: 没有运算符'>>'匹配这些操作数