std::ofstream does not accept const char * for << operator

1.6k views Asked by At

I've upgraded my C++ project from VS 2010 to 2015 and have some problems compiling it. Method header looks like following:

void CCodeGenerator::GenerateCode(const MachineExArray & AMachineExArray,
    const MachineArrays & AMachineArrays,
    std::ofstream & HeaderFile,
    std::ofstream & SourceFile)

There's a line:

std::string HeaderDefine = path(OutputFilename).filename().generic_string();

for (std::string::iterator Iter = HeaderDefine.begin(); Iter != HeaderDefine.end(); Iter++)
    *Iter = toupper((unsigned char)*Iter);

HeaderDefine = "__" + HeaderDefine + "_H__";

HeaderFile << "#ifndef " << HeaderDefine << "\n"; // <-- This one

Compiler stops here and says:

No operator "<<" matches these operands. Operand types are: std::ofstream << const char[9]

I wrote in C++ long ago, but from what I remember, std::ofstream is quite liberal and should accept most of simple types as input. Where's the problem?


When, at the beginning I write HeaderFile., It is immediately marked as error (red underline) and comment says, "Incomplete types are not allowed".

1

There are 1 answers

0
doctorlove On BEST ANSWER

An observation on the

"Incomplete types are not allowed"

. error.

#include <ostream>

int main()
{
    std::ofstream HeaderFile;
}

gives this error too, while this

#include <fstream>

int main()
{
    std::ofstream HeaderFile;
}

compiles in VS2015.