Sign in to follow this  

Boost smart pointers problem

Recommended Posts

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());
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

    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);

    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;


#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)

    // Close the file
    if (m_LogFile.is_open())
        this->LogRaw("\n- - - - -\nLog ended\n");

bool Logger::Init(const string& logFileName)
    m_FileName = logFileName;

    // Open file, std::ios::trunc | std::ios::out);
    if (!m_LogFile.is_open())
        return false;

    // Write header
    string 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>

#include <windows.h>
#include <dinput.h>

#include "../include/EngineDLL.h"

// Nifty macro
#define SAFE_RELEASE( p ) { if( p ) { ( p )->Release(); ( p ) = NULL; } }


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

Share this post

Link to post
Share on other sites
You can't put the statement g_Log->Init("log.txt"); outside of a function: you're not defining a variable (you did that on the line before), but calling an arbitrary function.

Pass parameters to your Logger constructor, move the initialization to main(), or have a look at Schwartz Counters.

Share this post

Link to post
Share on other sites
Original post by Slaru
Hum, I'm amazed I didn't know that... well, thanks. I kind of feel stupid now...

If that makes you feel better, it happened to me last month.

Share this post

Link to post
Share on other sites

No, a smart pointer is an object that acts like a pointer, so you use it as if it was a pointer, not an object.

AND, you declare it without specify the pointer type, but just the value type, because it's a pointer anyway, it's implicit. Don't go misleading people! (Unless I'm wrong, I would be very surprised)

boost::shared_ptr< Logger > g_Log

is correct. It is a smart pointer to a Logger object.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this