首页 > 解决方案 > 多个重载函数实例与参数列表匹配,我找不到错误发生的位置

问题描述

使用此代码时出现上述错误。

    //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;

}

我检查了我的函数和参数,我没有看到任何函数可以将其参数与其他任何地方混淆。如果它很简单而我只是没有看到它,请提前道歉。

标签: c++visual-studio-2010

解决方案


正如 cigien 和 Peter 的评论已经指出的那样:声明和定义的getname()参数不匹配。要解决此问题,请更改行

void getname(ofstream);

void getname(ofstream&);

注意&后面ofstream

此外,任何获得ofstreamas 参数的函数都应该通过引用获得(即 asofstream&而不仅仅是ofstream),因为没有复制构造函数 forofstream并且任何按值传递的尝试ofstream都会导致编译错误。


推荐阅读