0

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.

5
  • 2
    Your declaration of getname doesn't have a &, but your definition does. Commented Feb 19, 2021 at 1:42
  • 1
    Before main() you have a declaration void getname(ofstream). After main() you have a definition of void getname(ofstream &pfile). Note the missing & in the first. That's the problem in your code - why it gives that specific error with your compiler, I don't know (a std::ofstream cannot be passed by value, so the first declaration would not compile). Commented Feb 19, 2021 at 1:43
  • 1
    This doesn’t address the question, but get in the habit of initializing objects with meaningful values rather than default initializing them and immediately overwriting the default values. In this case, that means changing ofstream pfile; pfile.open(“Profile.txt”); to ofstream pfile(“Profile.txt”);. Commented Feb 19, 2021 at 2:49
  • The compiler's error message tells you exactly where the error occurs. Recent compilers even tell you the name and prototypes of the conflicting overloads. Commented Feb 19, 2021 at 8:46
  • Thats very helpful thank you for the clarification Commented Feb 19, 2021 at 17:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.