Home > Net >  More than one instance of overloaded function matches the argument list and I can't find where
More than one instance of overloaded function matches the argument list and I can't find where

Time:09-17

I get the above error when I use this code.

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

}

I've checked my functions and arguments and I don't see any function that its can be confusing its arguments with anywhere else. Apologies in advance if its something simple and I just didn't see it.

CodePudding user response:

As the comments by cigien and Peter already pointed out: The declaration and the definition of getname() have mismatched parameters. To fix that, change the line

void getname(ofstream);

to

void getname(ofstream&);

Note the & after ofstream.

Furthermore, any function that gets an ofstream as parameter should get that by reference (i. e. as ofstream& and not just ofstream), because there is no copy constructor for ofstream and any attempt to pass ofstream by value will cause compile errors.

  • Related