Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

some errors creating a logger class.

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

I'm trying to create a logger class for logging different events that happen. Now, the class I have seems OK but when compiling I get these two weird errors.

 

1>c:\rosario\lucifir\documents\visual studio 2008\projects\irrengine\irrengine\ilogger.cpp(10) : error C2758: 'Discord::IrrUtility::ILogger::outLogger' : must be initialized in constructor base/member initializer list
1>        c:\rosario\lucifir\documents\visual studio 2008\projects\irrengine\irrengine\ilogger.h(28) : see declaration of 'Discord::IrrUtility::ILogger::outLogger'
1>c:\rosario\lucifir\documents\visual studio 2008\projects\irrengine\irrengine\ilogger.cpp(10) : error C2758: 'Discord::IrrUtility::ILogger::DataStream' : must be initialized in constructor base/member initializer list
1>        c:\rosario\lucifir\documents\visual studio 2008\projects\irrengine\irrengine\ilogger.h(29) : see declaration of 'Discord::IrrUtility::ILogger::DataStream'

here's my logger .h file

 

header

#ifndef ILOGGER_H
#define ILOGGER_H

#include <string>
#include <fstream>
#include <sstream>
#include "IOSUtility.h"

using namespace std;

namespace Discord
{
namespace IrrUtility
{
	typedef enum LOGGER_MESSAGES
	{
		LM_DEFAULT = 0,			// default message
		LM_CRITICAL = 1,		// critical
		LM_INFORMATION = 2,		// information (same as default)
		LM_WARNING = 3,			// a warning.
		LM_UNKNOWN = 4			// something unknown happened.
	};
	// class to log basic information.
	class ILogger
	{
	protected:
		
		ofstream &outLogger;		// the file to open.
		stringstream& DataStream;	// the data stream.
		static ILogger* m_instance; // the instance.
		ILogger();
	public:
		// get the instance of the logger.
		static ILogger* getInstance();

		// create the logger.
		bool createLogger(const char* fileName);

		// add a critical message.
		void printCriticalMessage(const char* str);	// this function will print a critical message to the log file.

		// print information.
		void printInformation(const char* str);

		// print warning.
		void printWarning(const char* str);

		// this updates the data.
		void updateLogger();

		// open the logger.
		void openLogger();

		// close the logger.
		void closeLogger();


	};
}
}
#endif

now for the source file.

 

source

#include "ILogger.h"

namespace Discord
{
namespace IrrUtility
{
	ILogger* ILogger::m_instance = 0;

	ILogger::ILogger()
	{
		
	}

	ILogger* ILogger::getInstance()
	{
		if(m_instance == NULL)
		{
			m_instance = new ILogger();
		}
		return m_instance;
	}

	bool ILogger::createLogger(const char* fileName)
	{
		outLogger.open(fileName,ios::app | ios::out);
		return true;
	}

	void ILogger::printInformation(const char* str)
	{
		DataStream << str << endl;
	}
	void ILogger::closeLogger()
	{
		outLogger.close();
	}
}
}

if there's a better way to do this, please let me know.

Share this post


Link to post
Share on other sites
Advertisement

The two references in the ILogger class has to be initialized. You cannot have uninitialized references, they have to be initialized to refer to existing objects. The only way to initialize them would be in the constructor initializer list, and you then probably have to pass two existing objects to the constructor that the references can be bound to.

 

    ILogger::ILogger(ofstream &outLogger, stringstream &DataStream) : outLogget(outLogger), DataStream(DataStream)
    {
        
    }

Now, when constructing the ILogger class, pass an existing ofstream and an existing stringstream to the constructor so the references can be bound to something.

 

Whether that is the preferred solution in a bigger sense is another question. Do you really need the outLogger and DataStream members to be references instead of objects?

Edited by Brother Bob

Share this post


Link to post
Share on other sites

well, problem is, they have to be refrences, i've noticed that I get errors stating about accessing protected variables if ofstream out isn't a refrence

Share this post


Link to post
Share on other sites
>c:\program files (x86)\microsoft visual studio 9.0\vc\include\sstream(333) : error C2248: 'std::basic_streambuf<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_streambuf<_Elem,_Traits>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\streambuf(22) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::operator ='
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        This diagnostic occurred in the compiler generated function 'std::basic_stringbuf<_Elem,_Traits,_Alloc> &std::basic_stringbuf<_Elem,_Traits,_Alloc>::operator =(const std::basic_stringbuf<_Elem,_Traits,_Alloc> &)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Alloc=std::allocator<char>
1>        ]
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\fstream(548) : error C2248: 'std::basic_streambuf<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_streambuf<_Elem,_Traits>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\streambuf(22) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::operator ='
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        This diagnostic occurred in the compiler generated function 'std::basic_filebuf<_Elem,_Traits> &std::basic_filebuf<_Elem,_Traits>::operator =(const std::basic_filebuf<_Elem,_Traits> &)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>Build log was saved at "file://c:\Rosario\lucifir\Documents\Visual Studio 2008\Projects\IrrEngine\IrrEngine\Debug\BuildLog.htm"
1>DEngine - 2 error(s), 0 warning(s)

I get these errors when doing it like this:

 

 

    header:

#ifndef ILOGGER_H
#define ILOGGER_H

#include <string>
#include <fstream>
#include <sstream>
#include "IOSUtility.h"

using namespace std;

namespace Discord
{
namespace IrrUtility
{
	typedef enum LOGGER_MESSAGES
	{
		LM_DEFAULT = 0,			// default message
		LM_CRITICAL = 1,		// critical
		LM_INFORMATION = 2,		// information (same as default)
		LM_WARNING = 3,			// a warning.
		LM_UNKNOWN = 4			// something unknown happened.
	};
	// class to log basic information.
	class ILogger
	{
	protected:
		
		fstream outLogger;		// the file to open.
		stringstream DataStream;	// the data stream.
		static ILogger* m_instance; // the instance.
		ILogger();
	public:
		// get the instance of the logger.
		static ILogger* getInstance();

		// create the logger.
		bool createLogger(const char* fileName);

		// add a critical message.
		void printCriticalMessage(const char* str);	// this function will print a critical message to the log file.

		// print information.
		void printInformation(const char* str);

		// print warning.
		void printWarning(const char* str);

		// this updates the data.
		void updateLogger();

		// open the logger.
		void openLogger();

		// close the logger.
		void closeLogger();

		// sets the stream source.
		void setStreamSource(stringstream  s)
		{
			DataStream = s;
		}

		void setFileSource(fstream  o)
		{
			outLogger = o;
		}

	};
}
}
#endif

    cpp file

 

#include "ILogger.h"

namespace Discord
{
namespace IrrUtility
{
	ILogger* ILogger::m_instance = 0;

	ILogger::ILogger()
	{
	}

	ILogger* ILogger::getInstance()
	{
		if(m_instance == NULL)
		{
			m_instance = new ILogger();
		}
		return m_instance;
	}

	bool ILogger::createLogger(const char* fileName)
	{
		outLogger.open(fileName,ios::app | ios::out);
		return true;
	}

	void ILogger::printInformation(const char* str)
	{
		DataStream << str << endl;
	}
	void ILogger::closeLogger()
	{
		outLogger.close();
	}
}
}

so, yea, not sure. I used a data stream in another program and it didn't give those errors. weird

Share this post


Link to post
Share on other sites

You cannot copy stream objects, ao anything involving passing a stream by value or assigning one stream to another is forbidden. If you need to set the streams dynamically, then use pointers. Pointers can be changed to point somewhere else, which is why you need pointers instead of references.

Share this post


Link to post
Share on other sites

You could use a smart pointer like this

class ILogger {
public:
	ILogger() : outLogger(new std::ofstream()) {}
	std::shared_ptr<std::ofstream> outLogger;
}

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!