1
votes

I'm fairly new to C++ and when I was trying to compile my project, Visual Studio 2010 Express encountered a fatal error saying "Cannot open include file: 'ofstream': No such file or directory" The source code for one of my implementation files (where I'm experiencing the error) is as follows:

#include "Character.h"
#include <iostream>
#include <ofstream>

using namespace std;

ofstream characterSave;

// Sets the character's name to what the user inputted
void Character::setName(string name)
{
    // Set characterName to equal what user inputted
    characterName = name;
}

// Output the character's name when called
string Character::getName()
{
    // Return characterName
    return characterName;
}

// Save the user's data to save.dat
void Character::saveToFile()
{
    // Creates new save file
    characterSave.open("character.dat");

    // Writes character's name to first line of save file
    characterSave << characterName << endl;

    // Closes save file
    characterSave.close();
}

I've searched around online, and I cannot find anyone else with this problem. Could it be my local copy of Visual Studio Express? Any help would be much appreciated.

1
Google for ofstream, first result shows you need header <fstream> - Peter Wood

1 Answers

3
votes

You need

#include <fstream>

That's where ofstream is declared.