• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ file output question

AgentZap

Senior member
In Visual C++ .net when I try to compile the code I get the error C2664 'write' cannot convert parameter 1 from 'std;;ofstream' to 'std;;ofstream' (i used semicolons instead of colons so the forum doesn't convert it to emoticons)

Below is an example of the code it doesn't like. What am I doing wrong? Is there a better way to pass file output to a modularized function yet open the file for output inside main()?

#include <iostream>
#include <fstream>

using namespace std;

void write(ofstream);

void main()
{
ofstream outfile;
outfile.open("C:\\blah.txt");

write(outfile);
}

void write(ofstream outfile)
{
outfile << "hi";
}
 
I was under the impression that matching prototypes exactly to functions was important. Perhaps that's causing your problem?
Also, any specific reason for the "\\" instead of just "\" in the file name? I don't remember if the \\ is okay.
 
Originally posted by: atiyeh
void write(ofstream); try

void write(ofstream outfile);

it's bad form to just declare the type.


Point noted on the form aspect, but making that change results in the same error.
 
Originally posted by: Legendary
I was under the impression that matching prototypes exactly to functions was important. Perhaps that's causing your problem?
Also, any specific reason for the "\\" instead of just "\" in the file name? I don't remember if the \\ is okay.

I am sorry I am not following you regarding prototype matching. Would you be able to provide me with an example of what you mean?

In my programming course the teacher has always used the \\ in his notes to us so thats why I use it. It has never been a problem before.

 
Got it!! jimithing2077 let me know where I was going wrong over on the programming forum. I needed an ampersand to declare the pointer

void write(ofstream &);

void write(ofstream & outfile)
{
outfile << "hi";
}
 
Back
Top