我有一个基本功能:
TokenType getToken(istream *in, string& recognized){
char token;
in >> token;
if (token=='#'){
in.ignore('\n');
in >> token;
}
return T_UNKNOWN;
}
(TokenType
只是一个枚举。)
由于某些原因,in >> token;
当用G ++编译时,这两行都给我这个错误:
error: invalid operands of types ‘std::istream* {aka std::basic_istream<char>*}’ and ‘char’ to binary ‘operator>>’
为什么编译器会抛出此错误?我希望能够char
从in
指向的流中提取一个,如果char
是磅号,则跳到下一行。
为了后代,我包括:
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
using namespace std;
您需要取消引用istream指针
(*in) >> token;
in->ignore('\n');
或者更改为引用而不是指针。
TokenType getToken(istream & in, string& recognized);
您将必须通过取消引用指针来更改调用函数的方式。
getToken(*in, recognized);
正如0x499602D2指出的那样,使用in->ignore('\n');
对您的用法没有意义,您需要使用:
in->ignore(std::numeric_limits<std::streamsize>::max(), '\n');
在找到新的换行符之前,它将忽略最多以字符为单位的最大流大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句