c++ - 多个重载函数实例与参数列表匹配,我找不到错误发生的位置
问题描述
使用此代码时出现上述错误。
//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;
}
我检查了我的函数和参数,我没有看到任何函数可以将其参数与其他任何地方混淆。如果它很简单而我只是没有看到它,请提前道歉。
解决方案
正如 cigien 和 Peter 的评论已经指出的那样:声明和定义的getname()
参数不匹配。要解决此问题,请更改行
void getname(ofstream);
至
void getname(ofstream&);
注意&
后面ofstream
。
此外,任何获得ofstream
as 参数的函数都应该通过引用获得(即 asofstream&
而不仅仅是ofstream
),因为没有复制构造函数 forofstream
并且任何按值传递的尝试ofstream
都会导致编译错误。
推荐阅读
- asp.net - 动态 SQL 语句的语法不正确
- javascript - setState 未正确更新状态
- arrays - 类型化类的 Typescript 数组
- c# - 从 LINQ 获取数据并作为公共保存在数组中
- python-3.x - 为什么我的 root.after 命令不能循环工作?
- php - 基于字符串在 PHP 中创建随机颜色
- javascript - JavaScript/TypeScript 中有没有办法在一个对象中添加两个数组并对它们进行排序?
- scala - 为什么转换为集合时,Scala for 循环会产生自动框整数?
- java - SpringBoot for my Mac 中的通配符子域
- git - Does sparse-checkout act on tracked file?