Jump to content
  • Advertisement
Sign in to follow this  
NovaCaine

Trouble with Singletons

This topic is 4831 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

Hey all, Trying to write a basic logging system, where i send it a string and it will write it to a file. LogFile.h
#pragma once

#pragma warning(disable : 4996) // Without this i get a warning on sprintf

#include <stdio.h>
#include <fstream>

class CLogFile {
	public:
		~CLogFile();

		// Singleton Accessor
		static CLogFile* GetInstance();

		// Initialises the Log File
		void Init();

		// Logs an error message
		void LogError( const char* p_strError );

		// Logs a Message
		void LogMessage( const char* p_strMessage );

		// Closes the files and flushes last of data to them
		void Close();

		std::ofstream	m_filErrorLog;	// Error log File
		std::ofstream	m_filMsgLog;	// Message Log File

	private:
		static CLogFile*	m_spLogFile;
		CLogFile();

};

LogFile.cpp
#include "LogFile.h"

CLogFile::CLogFile() {
}

CLogFile::~CLogFile() {
	if(m_spLogFile) {
		delete m_spLogFile;
	}
}

CLogFile* CLogFile::GetInstance() {
	if( m_spLogFile == NULL ) {
		m_spLogFile = new CLogFile();
	}

	return m_spLogFile;
}

// Initialises the Log File
void CLogFile::Init() {
	m_filErrorLog.open( "ErrorLog.txt", std::ios::out | std::ios::trunc );
	m_filMsgLog.open( "MsgLog.txt", std::ios::out | std::ios::trunc );
}

// Logs an error message
void CLogFile::LogError( const char* p_strError ) {
	char szBuffer[1024];

	sprintf( szBuffer, "ERROR: %s\n", p_strError );

	m_filErrorLog << szBuffer;
	m_filErrorLog.flush();
}

// Logs a Message
void CLogFile::LogMessage( const char* p_strMessage ) {
	char szBuffer[1024];

	sprintf( szBuffer, "Message: %s\n", p_strMessage );

	m_filMsgLog << szBuffer;
	m_filMsgLog.flush();
}

// Closes the files and flushes last of data to them
void CLogFile::Close() {
	m_filErrorLog.flush();
	m_filErrorLog.close();

	m_filMsgLog.flush();
	m_filMsgLog.close();

}

it all compiles fine, but i get an error while linking LogFile.obj : error LNK2020: unresolved token (0A0002EA) "private: static class CLogFile * CLogFile::m_spLogFile" (?m_spLogFile@CLogFile@@0PAV1@A) LogFile.obj : error LNK2001: unresolved external symbol "private: static class CLogFile * CLogFile::m_spLogFile" (?m_spLogFile@CLogFile@@0PAV1@A) Any ideas? this is written basically the same as my game class which is also a singleton, but i dont get any errors there. Have i missed something?

Share this post


Link to post
Share on other sites
Advertisement
You need to define the static variable. At this point you've only declared it. In your source file put

CLogFile * CLogFile::m_spLogFile;


at namespace level (not in a function or a class).

Share this post


Link to post
Share on other sites
Hmmmm, maybe i put it on the wrong line? Tried doing that with my game class and it gave me an error. Thanks for that.

edit: My bad, already had it in my game class, just one of those days

[Edited by - NovaCaine on September 22, 2005 8:42:29 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!