[英]More than one instance of overloaded function matches the argument list and I can't find where the error happens
使用此代码时出现上述错误。
//Programming Assignment 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function Prototypes
void getname(ofstream);
//void Evaluate_holesterol(ofstream);
//void Evaluate_BMI(ofstream);
//void Evaluate_bloodpressure(ofstream);
int main()
{
//Open output file
ofstream pfile;
pfile.open("Profile.txt");
getname(pfile);
//Evaluate_holesterol(pfile);
//Evaluate_BMI(pfile);
//Evaluate_bloodpressure(pfile);
//pfile.close();
system("pause");
return 0;
}
//Function to get patient's name
void getname(ofstream &pfile)
{
string name;
int age;
cout<<"What is the patient's full name (middle initial included)?";
getline(cin, name);
cout<<endl<<"What is the patient's age?";
cin>>age;
string line = "Patient's Name: ";
string ageline = "Patient's Age: ";
pfile<<line+name<<endl;
pfile<<age<<endl;
}
我检查了我的函数和 arguments 并且我没有看到任何 function 可能会将其 arguments 与其他任何地方混淆。 如果它很简单而我只是没有看到它,请提前道歉。
正如 cigien 和 Peter 的评论已经指出的那样: getname()
的声明和定义的参数不匹配。 要解决此问题,请更改行
void getname(ofstream);
至
void getname(ofstream&);
注意ofstream
之后的&
。
此外,任何以ofstream
为参数的 function 都应通过引用获得该参数(即,作为ofstream&
而不仅仅是ofstream
),因为没有用于ofstream
的复制构造函数,并且任何按值传递ofstream
的尝试都会导致编译错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.