C++ 类型为';标准:istream*';和';char';到二进制';运营商>&燃气轮机';
我有一个基本的功能:C++ 类型为';标准:istream*';和';char';到二进制';运营商>&燃气轮机';,c++,c++11,C++,C++11,我有一个基本的功能: TokenType getToken(istream *in, string& recognized){ char token; in >> token; if (token=='#'){ in.ignore('\n'); in >> token; } return T_UNKNOWN; } (TokenType只是一个枚举。) 由于某种原因,两个都在>>标记中行给了我
TokenType getToken(istream *in, string& recognized){
char token;
in >> token;
if (token=='#'){
in.ignore('\n');
in >> token;
}
return T_UNKNOWN;
}
(TokenType
只是一个枚举。)
由于某种原因,两个都在>>标记中当使用G++编译时,代码>行给了我这个错误:
error: invalid operands of types ‘std::istream* {aka std::basic_istream<char>*}’ and ‘char’ to binary ‘operator>>’
您需要取消对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');
in->ignore(std::numeric_limits::max(),'\n');
它将忽略最大字符流大小,直到找到新行字符。为什么不在其代码中使用std::istream&
?in->ignore('\n')
)没有任何意义。我想他的意思是in->ignore(std::numeric\u limits::max(),'\n')
。如果函数被调用任意次数,取消对指针的引用是否会导致奇怪的事情发生?比方说,如果指针指向ifstream
并且函数是在(!file.eof())
循环时从调用的,这会留下一个无限循环吗?指针仍然应该指向ifstream
对象,而不管您在循环中调用它。@MowDownJoe,当然,当(!file.eof())
时,您不会在任何上下文中编写。这根本没有任何意义。
in->ignore(std::numeric_limits<std::streamsize>::max(), '\n');