2014-02-22 30 views
1

我有一个基本的功能:的类型 '的std :: istream的*' 和 '炭' 二进制 '操作>>' 无效操作数

TokenType getToken(istream *in, string& recognized){ 
    char token; 
    in >> token; 
    if (token=='#'){ 
     in.ignore('\n'); 
     in >> token; 
    } 
    return T_UNKNOWN; 
} 

TokenType只是一个枚举。)

error: invalid operands of types ‘std::istream* {aka std::basic_istream<char>*}’ and ‘char’ to binary ‘operator>>’ 

为什么编译器引发此错误:由于某些原因,当与G ++编译时同时in >> token;线的给我这个错误?我希望能够从in指向的流中提取一个char,然后如果该char是英镑符号,则跳到下一行。

为子孙后代着想,我包括:

#include <cstdlib> 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <regex> 

using namespace std; 
+0

为什么不只是一个'std :: istream&'? – chris

回答

5

您需要取消引用您的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'); 

这会忽略到最大流大小的字符,直到一个新的行字符被找到。

+1

'in-ignore('\ n')'在他的代码中没有任何意义。我认为他的意思是' - >忽略(std :: numeric_limits :: max(),'\ n')'。 – 0x499602D2

+0

取消引用指针会导致奇怪的事情发生,如果函数被调用任意次数?比方说,如果指针指向一个'ifstream',并且函数正在从while(!file.eof())'循环中调用,那么会留下一个无限循环吗? – MowDownJoe

+0

无论您是否在循环中调用该指针,该指针仍应指向“ifstream”对象。 – GWW

相关问题