Q:

error: ‘logFileObj’ does not name a type

I have a file named global.h whose contents are:
#define DEPTH 10
#define LOGGING     //to log the progress of the program. 
#ifdef LOGGING
#include <fstream>
#include <string>
extern std::string logFileName;
extern std::ofstream logFileObj;
#endif

Also main.cpp:
#include "global.h"

using namespace std;

#ifdef LOGGING
string logFileName = ".log";
ofstream logFileObj;
logFileObj.open(logFile);    //line 13
logFileObj<<"depth: "<<DEPTH<<endl;    //line 14
#endif

I am constantly getting the following error in compilation:
src/main.cpp:13:1: error: ‘logFileObj’ does not name a type
src/main.cpp:14:1: error: ‘logFileObj’ does not name a type

Any help appreciated. 

A:

C++ does not allow operation outside function.  C++ allows you define variable globally but you need to put operations inside functions. 
If I read your question correctly, you just need a function and call it when you need to:
#include <fstream>
#include <utility>
#include <string>

template<typename T>
void WriteLog(const std::string& log_file_name, const std::string& prefix, const T& data)
{
  std::ofstream log_file_handler(log_file_name.c_str(), std::ios::app);  // if you use C++11, you could use string directly
   log_file_handler << prefix << data << std::endl;
}

usage:
WriteLog<int>("app.log", "depth:", DEPTH);

