I am trying to print into a file in C++ and for some reason I keep getting this weird error:
error C2061: syntax error : identifier
‘ofstream’
I included the following:
#include <fstream>
#include <iostream>
This is my function:
void Date::PrintDate(ofstream& resultFile) const
{
resultFile << m_day << "/" << m_month << "/" << m_year;
}
I am using namespace std.
I figured it out, it was all because I did not include the file in the right way.
Use
std::ofstreamThis is because we have to explicitly specify which ofstream we are talking about. Since the standard namespace
stdcontains the nameofstream, it has to be explicitly told to the compilerThere are essentially two ways:
Just before all the include files in the .cpp file, have a using directive
1:
using namespace std;or
2: prefix each name from the namespace std with
std::EDIT 2:
Your revised function declaration should look as follows as Option 1 (from above) is a preferred way to avoid global namespace pollution usually