Jump to content
  • Advertisement
Sign in to follow this  
Slaru

Boost smart pointers problem

This topic is 4795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
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.

Share this post


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

Slaru

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!