Problem with template

Started by
7 comments, last by Austrian Coder 19 years, 11 months ago
Hi. I am developing on linux a plugin for an application. And i want to use a simple but powerfull loger. So i am using this:

// ==================================
//!  A log class. 
/*!
	With this nice util dxr3plugin generates and mange a log file. In this
	file the developer/enduser can find informations and can find errors,
	problems and ohter stuff.
*/
class cLog : public Singleton
{
public: 
	cLog();		// use default log file
	cLog(std::string FileName);
	
	~cLog()		{ Close(); }
	
	void SetForceFlush(const bool v)	{ m_ForeFlush = v; }
	bool GetForceFlush() const			{ return m_ForeFlush; }
	
	// nice operator overloading 
	template cLog &operator<<(const T& output)
	{
		m_LogStream << output;
		if (m_ForeFlush) m_LogStream.flush();
		return *this;
	}
	
private:
	std::ofstream	m_LogStream;
	bool			m_LogOpen;
	bool			m_ForeFlush;
	
	void Open(std::string Filename);	// with this function we open our logfile
	void Close();				// with this function we close our logfile
};
 
No i call the loger so: cLog::Instance() << (char*)"This is a test"; And i get this error:

In file included from dxr3interface.c:77:
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/ios_base.h: In
   copy constructor `std::basic_ios
   >::basic_ios(const std::basic_ios >&)'':
dxr3singleton.h:18:   instantiated from `static T Singleton::Instance() [with T = cLog]''
dxr3log.h:28:   instantiated from `cLog& cLog::operator<<(const T&) [with T = char*]''
dxr3interface.c:77:   instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/ios_base.h:668: error: `
   std::ios_base::ios_base(const std::ios_base&)'' is private
dxr3singleton.h:18: error: within this context
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/streambuf: In copy
   constructor `std::basic_filebuf
   >::basic_filebuf(const std::basic_filebuf >&)'':
dxr3singleton.h:18:   instantiated from `static T Singleton::Instance() [with T = cLog]''
dxr3log.h:28:   instantiated from `cLog& cLog::operator<<(const T&) [with T = char*]''
dxr3interface.c:77:   instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/streambuf:922: error: `
   std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const
   std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits =
   std::char_traits]'' is private
 
What could be wrong? Thanks, for any help, Christian
Advertisement
You'll have to post your Singleton code too.

I'm not sure why you're having problems, but I thought I'd throw in a Shameless Plug: Logging Control API

My Logging API is:
- flexible
- extensible
- thread-safe
- cross-platform
- has the ability to be compiled completely out of your application
- has neat syntax

Regards,
Jeff


[ CodeDread ]

[edited by - rypyr on May 21, 2004 12:04:33 PM]
Ok.. here is my singleton:

	template	class Singleton	{		protected :			Singleton() {}			virtual ~Singleton() {}		public:			static T GetInstance()  			{				static T m_Instance;				return m_Instance;			}	};  


Oh and where can i find the source of your logginsystem? Becaue you only have win dll's, but i need it for Linux.

[edited by - Austrian Coder on May 21, 2004 12:43:56 PM]
The problem seems to stem from your Singleton-class. The Singleton''s Instance method returns T by value, which makes a copy of the T, and your cLog class contains an ostream which doesn''t have a copy-constructor. In other words, when you call cLog::Instance(), the compiler wants to create a copy of your Singleton cLog, which in turns wants to copy the std::ofstream, which fails.

Simple solution: make the Singleton''s Instance method return a T& instead of a T.
Which is something your Singleton really should do in any case, otherwise it''s not really a Singleton (everytime you call Instance now a new copy is created).

- neophyte
I've probably missed something here, but shouldn't you be supplying a template argument such as "template < class T > "?

[edited by - xCube on May 21, 2004 1:32:22 PM]
quote:Original post by Austrian Coder
Oh and where can i find the source of your logginsystem? Becaue you only have win dll''s, but i need it for Linux.


Austrian Coder,

If you are interested in compiling the LoggingControl API into a shared object library, I would be interested to share the source with you. It shouldn''t be a difficult task since I already wrote the source cross-platform and did a test compile under cygwin, it''s just a matter of compiling it into a shared object library under Linux (I don''t have a Linux platform available to me). Then I can post a link to the .so file going forward.

You could go to CodeDread.com and sign up at the forums and then either PM me or post your request in here.

Regards,
Jeff


[ CodeDread ]
quote:Original post by xCube
I''ve probably missed something here, but shouldn''t you be supplying a template argument such as "template < class T > "?

[edited by - xCube on May 21, 2004 1:32:22 PM]


Yes, it just didn''t come out in the forums because he didn''t use the source tags. Do an edit/quote on his original post to see what I mean.

[ CodeDread ]
I thought it must have been more difficult
@Neophyte: Thanks, now it works

@rypyr: It would be a shared object, but with full sourcecode. But as the problem is solved now, i think that i dont need your logger anymore, but thanks.

This topic is closed to new replies.

Advertisement