错误:运算符不匹配

时间:2018-12-13 13:53:31

标签: c++

我确实有这段代码

    // Open the file
    infile.open(filename);
    if (!infile) {
      stockmsg << "InputData::read: Failed trying to open file " << filename << endl;
      return NULL;
    }

    // Header line confirms if right kind of file
    // In the future, might need to check the version number
    count++;
    infile >> keyword;
    if (keyword != "#STOCK") {
      stockmsg << "InputData::read : input file header line unrecognised" << endl;
      return NULL;
    }
    getline(infile,nextline,'\n');  // Discard the rest of the line

    // Read the file line by line
    etree = NULL;
    while (status == READ_SUCCESS) {
      count++;

// +++++++++++++++
// KEYWORDS stage
// +++++++++++++++

      // When in KEY_WORDS mode, try to get another line
      if (stage == KEY_WORDS) {
        if (getline(infile,nextline,'\n') == 0) {
          stockmsg << "InputData::read : unexpected end of file at line " << count << endl;
          status = READ_FAILURE;
          break;
        }

编译时,我收到一条错误消息

error: no match for 'operator==' 
(operand types are 'std::basicistream<char>' and 'int')
if (getline(infile,nextline,'\n')==0) {

我不确定该如何解决。

1 个答案:

答案 0 :(得分:4)

这就是它的意思。

您做了一个(<*>),然后尝试将结果(流)与getline进行比较。

那是行不通的。流和整数不能相互比较。但是,可以在流和布尔值之间使用魔术。

所以,写这个:

0

当流状态良好时,if (!getline(infile,nextline,'\n')) { 表达式将是“真实的”。

(我在某种程度上简化了流的布尔值的工作方式,但是现在就可以了。)