Hi all,
i am new to C++, i was trying to declare a class for writing a log file. everyting was fine until i separate interface from implementation! below are the codes :
Log.H
----------------------------------------------
#ifndef LOG_H
#define LOG_H
class Log {
public:
Log ();
~Log();
void appendLog ( string );
void appendLog ( unsigned long );
string readLog ();
void closeFile ();
private:
void openFile ();
ofstream outFile;
};
#endif
----------------------------------------------
Log.cpp
----------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
#include <fstream>
using std:
fstream;
#include <cstdlib>
#include <string>
using std::string;
#include "Log.h"
#define fName "try.log"
Log::Log(){ openFile (); }
Log::~Log(){
if (outFile.is_open())
closeFile();
}
void Log::appendLog ( string logMsg ){
outFile << logMsg << "\r\n";
}
void Log::appendLog ( unsigned long logMsg ){
outFile << logMsg << "\r\n";
}
string Log::readLog (){
string str;
return str;
}
void Log:
penFile (){
outFile.open ( fName, ios::app );
if (!outFile){
cerr << "File Could Not Be Opened" << endl;
exit(1);
}
}
void Log::closeFile (){
outFile.close();
}
----------------------------------------------
i was facing problem on compilation when i include this header file to other class... i have tried forward declaration but it bring worst.
Thanks,
zonique
i am new to C++, i was trying to declare a class for writing a log file. everyting was fine until i separate interface from implementation! below are the codes :
Log.H
----------------------------------------------
#ifndef LOG_H
#define LOG_H
class Log {
public:
Log ();
~Log();
void appendLog ( string );
void appendLog ( unsigned long );
string readLog ();
void closeFile ();
private:
void openFile ();
ofstream outFile;
};
#endif
----------------------------------------------
Log.cpp
----------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
#include <fstream>
using std:
#include <cstdlib>
#include <string>
using std::string;
#include "Log.h"
#define fName "try.log"
Log::Log(){ openFile (); }
Log::~Log(){
if (outFile.is_open())
closeFile();
}
void Log::appendLog ( string logMsg ){
outFile << logMsg << "\r\n";
}
void Log::appendLog ( unsigned long logMsg ){
outFile << logMsg << "\r\n";
}
string Log::readLog (){
string str;
return str;
}
void Log:
outFile.open ( fName, ios::app );
if (!outFile){
cerr << "File Could Not Be Opened" << endl;
exit(1);
}
}
void Log::closeFile (){
outFile.close();
}
----------------------------------------------
i was facing problem on compilation when i include this header file to other class... i have tried forward declaration but it bring worst.
Thanks,
zonique