【问题标题】:why am I getting an error when I pass an ifstream&?为什么我在传递 ifstream& 时会出错?
【发布时间】:2011-09-03 13:36:57
【问题描述】:

我正在尝试编写一个使用 ifstream 和扫描仪读取文本文件的简单程序。出于某种原因,我收到此错误:"In passing argument 1 of 'bool ReadVector(std::ifstream&, Vector<double>&)'"。知道我做错了什么吗?

#include <iostream>
#include <fstream>
#include <string>
#include "scanner.h"
#include "genlib.h"
#include "simpio.h"
#include "vector.h"

// prototype
bool ReadVector(ifstream & infile, Vector<double> & vec);

// main
int main(){
    Vector<double> vec;
    ifstream infile;
    infile.open("SquareAndCubeRoots.txt");
    if (infile.fail()) Error("Opening file screwed up");
    bool foo = ReadVector(&infile, &vec); // stub
    cout << foo;
    infile.close();
    return 0;
}

// stub
bool ReadVector(ifstream & infile, Vector<double> & vec){
    return true;
}

【问题讨论】:

  • 在这种情况下你真的不需要infile.close();。当ifstream 超出范围时会自动调用它。

标签: c++ parameter-passing ifstream


【解决方案1】:

ReadVector 接受引用,但您给出的是指针。只需拨打电话

bool foo = ReadVector(infile, vec);

【讨论】:

    【解决方案2】:

    你试图传递一个指针,而参数是一个引用。删除地址运算符 (ReadVector(infile, vec))。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多