Hello,
I'm using
Boost's smart pointers. Basically, I have a class (Logger) and I typedef a boost::scoped_ptr for it like so:
typedef boost::scoped_ptr<Logger> Logger_ptr;
I then declare a global pointer to a Logger_ptr:
extern Logger_ptr g_Log;
In the implementation file (Logger.cpp), I initialize the g_Log like this:
Logger_ptr g_Log(new Logger());
g_Log->Init("log.txt");
The g_Log->Init() function is just to startup the class.
My problem is that the compiler seems to think the g_Log variable isn't a Logger_ptr type, but something else (intellisense says it's an int).
I will post the files, they are fairly short.
Logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include "Core.h"
//==============================================================================
// Logger class
//==============================================================================
class Logger
{
public:
Logger();
~Logger();
bool Init(const string& logFileName);
bool IsInit() const;
void Flush(); // Flush the log to its file
void Log(const string& message, bool time = false,
unsigned int lineNum = __LINE__);
void LogWarning(const string& message, bool time = false,
unsigned int lineNum = __LINE__);
void LogError(const string& message, bool time = false,
unsigned int lineNum = __LINE__);
void LogRaw(const string& message);
private:
static void GetTime(string& timeStr);
bool m_Loaded;
string m_FileName;
std::ofstream m_LogFile;
};
typedef boost::scoped_ptr<Logger> Logger_ptr;
// Global logger object
extern Logger_ptr g_Log;
#endif
Logger.cpp
#include "Logger.h"
// Initialize global logger object
Logger_ptr g_Log(new Logger());
g_Log->Init("log.txt"); // This line has the errors!
//==============================================================================
// Logger class implementation
//==============================================================================
Logger::Logger() : m_Loaded(false)
{
}
Logger::~Logger()
{
// Close the file
if (m_LogFile.is_open())
{
this->LogRaw("\n- - - - -\nLog ended\n");
this->Flush();
m_LogFile.close();
}
}
bool Logger::Init(const string& logFileName)
{
m_FileName = logFileName;
// Open file
m_LogFile.open(m_FileName.c_str(), std::ios::trunc | std::ios::out);
if (!m_LogFile.is_open())
{
return false;
}
// Write header
string time;
this->GetTime(time);
m_LogFile << "Log started on " << time << "\n- - - - -\n" << std::endl;
// Success
m_Loaded = true;
return true;
}
// The rest is snipped, it's of no importance
Core.h - This is just an include file I include in all my files.
#ifndef CORE_H
#define CORE_H
// Standard Library Includes
#include <string>
using std::string;
#include <fstream>
#include <ctime>
#include <vector>
// Boost headers
#include <boost/smart_ptr.hpp>
// API/SDK
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include "../include/EngineDLL.h"
// Nifty macro
#define SAFE_RELEASE( p ) { if( p ) { ( p )->Release(); ( p ) = NULL; } }
#endif
The compiler spits this out:
src\Logger.cpp(5) : error C2143: syntax error : missing ';' before '->'
src\Logger.cpp(5) : error C2501: 'g_Log' : missing storage-class or type specifiers
src\Logger.cpp(5) : error C2371: 'g_Log' : redefinition; different basic types
i:\Radiix\Engine\src\Logger.h(47) : see declaration of 'g_Log'
The odd thing is that I do the exact same thing for another class and it works fine. I can't see what I'm doing differently though.
Any insight would be helpful. I can describe more if needed.
Slaru