为什么我在传递 ifstream& 时会收到错误?
我正在尝试编写一个简单的程序,该程序使用 ifstream 和扫描仪来读取文本文件。由于某种原因,我收到此错误:“传递 'bool ReadVector(std::ifstream&, Vector
。知道我做错了什么吗?
#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;
}
I'm trying to code a simple program that uses an ifstream and scanner to read a text file. For some reason I'm getting this error: "In passing argument 1 of 'bool ReadVector(std::ifstream&, Vector<double>&)'"
. Any idea what I've done wrong?
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ReadVector 接受引用,但您给出的是指针。只需致电
ReadVector accepts a reference, but you are giving a pointer. Just call
您试图传递一个指针,而参数是一个引用。删除地址运算符 (
ReadVector(infile, vec)
)。You're trying to pass a pointer, while the argument is a reference. Remove address-of operators (
ReadVector(infile, vec)
).