what's the difference betwwen these two ways to use std::ofstream

T

thinktwice

//case 1, works fine
function(..)
{
std::eek:fstream out;
out.open(file);
out << "error info" <<endl;
out.close();
}

//case 2, declare it in a class
class Logger
{
public:
Logger(std::wstring file, std::ios_base::eek:penmode mode /*undefined
too, why?*/){}
private:
std::eek:fstream out; //error c2079, uses undefined class
'std::basic_ofstream<_Elem, _Traits>'
}

btw, how to use ios_base:eek:penmode? is it declared in std namespace?
 
B

Bo Persson

thinktwice said:
//case 1, works fine
function(..)
{
std::eek:fstream out;
out.open(file);
out << "error info" <<endl;
out.close();
}

Just a guess: The function is in a cpp file that includes the
//case 2, declare it in a class
class Logger
{
public:
Logger(std::wstring file, std::ios_base::eek:penmode mode
/*undefined
too, why?*/){}
private:
std::eek:fstream out; //error c2079, uses undefined class
'std::basic_ofstream<_Elem, _Traits>'
}

A class declaration if often in a .h file. If so, it must also include
the necessary headers for names it uses.
btw, how to use ios_base:eek:penmode? is it declared in std namespace?

It is. There is a header <ios> that contains a lot of types and
constants used by the stream classes.


Bo Persson
 
T

thinktwice

thanks Persson , you are right,
i do include <fstream> in cpp file.
and the function implementation and the class definination is in the
header file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top