Boost smart pointers problem

Started by
5 comments, last by _the_phantom_ 18 years, 10 months ago
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
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hum, I'm amazed I didn't know that... well, thanks. I kind of feel stupid now...

Slaru
Quote: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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Deleted due to my mistake

[Edited by - Glass_Knife on June 11, 2005 7:33:50 AM]

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Glass-Knife:

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.
indeed persil, you are quite correct, the smart pointer class adds the pointer element its self to the type you specify.

This topic is closed to new replies.

Advertisement