Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:30:36+00:00 2026-05-26T18:30:36+00:00

I have a class in C++ on Linux that allows me to display messages

  • 0

I have a class in C++ on Linux that allows me to display messages on the console, depending on where the application is running. If it works in my computer, the messages displayed in console mode. Otherwise, everything is recorded in a text file for later viewing. And I created an extern object to use it. I wanted to try to make this class a singleton, but without success. I have this error appears to be when I come to compile my program:

error: invalid operands of types ‘Log *’ and ‘const char [12]’ to binary ‘operator <<‘

And also:

error: no match for ‘operator <<‘ in ‘logOutput’ << Buffer_T ….

I would like your opinion. Thank you in advance for your attention.

My functional class currently

typedef std::vector<unsigned char> Buffer_T;

class Log
{
public:
    Log();
    virtual ~Log();

    void init()
    {
    #indef FriendlyArm
        output = new ofstream("/home/arm/Log.txt");
    #else
        output = &cout;
    #endif
    }


    template<typename T>
    Log operator<<( T const& value )
    {
        (*output) << value;
        return *this;
    }

    Log& operator<<( std::ostream&(*f)(std::ostream&) )
    {
        (*output) << f;
        return *this;
    }

    Log& operator<<(Buffer_T& Buf)
    {
        if( Buf.size() > 0 )
        {
            for( unsigned int i = 0; i < Buf.size(); i++ )
            {
                if( Buf[i] >= 32 && Buf[i] <= 127 )
                {
                    (*output) << Buf[i];
                }
                else
                {
                    (*output) << "0x" << std::setfill( '0' ) << std::hex << std::setw( 2 ) << unsigned( Buf[i] );
                }
            }
        }
        return *this;
    }


private:
    ostream *output;
};
#endif /* LOG_H_ */

And here is my attempt to singleton

typedef std::vector<unsigned char> Buffer_T;

class Log
{
    public:
        static Log *createOrGet()
        {
            if(_unique == NULL)
            {
                _unique = new Log();
            }

            return _unique;
        }

        static void kill()
        {
            if(_unique != NULL)
            {
                delete _unique;
                _unique = NULL;
            }
        }

        void init()
        {
        #indef FriendlyArm
            output = new ofstream("/home/arm/Log.txt");
        #else
            output = &cout;
        #endif
        }

        template<typename T>
        Log operator<<( T const& value )
        {
            (*output) << value;
            return *this;
        }

        Log& operator<<( std::ostream&(*f)(std::ostream&) )
        {
            (*output) << f;
            return *this;
        }

        Log& operator<<(Buffer_T& Buf)
        {
            if( Buf.size() > 0 )
            {
                for( unsigned int i = 0; i < Buf.size(); i++ )
                {
                    if( Buf[i] >= 32 && Buf[i] <= 127 )
                    {
                         (*output) << Buf[i];
                    }
                    else
                    {
                        (*output) << "0x" << std::setfill( '0' ) << std::hex << std::setw( 2 ) << unsigned( Buf[i] );
                    }
                }
            }
            return *this;
        }

    private:
        Log();
        virtual ~Log();
        static Log *_unique;
        ostream *output;
};
Log *Log::_unique = NULL;
#endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:30:36+00:00Added an answer on May 26, 2026 at 6:30 pm

    Important: Don’t take it the wrong way, but you make some mistakes that show you’re not very experienced and singletons are very hard to use properly. In the case of a logger they are okay, just make sure to always use them as a last resort.

    I don’t have the time to go over why operator<< isn’t working for you, but here’s some tips on how to improve your singleton (you also seem to be asking for advice on that).

    The Instance function

    // The idiomatic way is to call this function 'Instance',
    // but this doesn't really matter.
    static Log& Instance() {
        static Log obj;
        return obj;
    }
    

    That’s called Meyer’s singleton. For single threaded application it’s great: the instance gets created on the first call of the function and automatically gets destroyed when the application closes (you don’t need a kill function).

    Your private virtual destructor

    I notice you have a private virtual destructor. You only need a virtual destructor when there’s at least one other virtual function in the class. This is not the case; make the destructor non-virtual.

    Enforcing a single instance

    You made the constructor private — that’s good, you are preventing me from directly creating multiple instances of the singleton. However you did not prevent me from making copies of the existing singleton. To also prevent that you also need to make the CopyConstructor and AssignmentOperator non-public:

    protected:
      Log();
      Log(const Log&); // CopyConstructor
      Log& operator=(const Log&); // AssignmentOperator
      ~Log();
    

    (The destructor should also be private in order to prevent me from deleting the only instance of the class.)

    Also notice I made them protected, not public. If you don’t know the difference, look it up (not enough space to explain here). I made them protected so you could inherit from the Log class if you need to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ class that compiles fine on linux with gcc and on
I have written a C++ class for Windows and Linux that creates a memory-mapped
I have the following code that works on Linux but doesn't work on Windows(VS2008)
I have a Linux C++ application that creates a JVM and makes JNI calls.
I have a class: public class ANote extends JDialog{...} In GNOME(Linux) it shows an
I have class method that returns a list of employees that I can iterate
I have class with a member function that takes a default argument. struct Class
First off I'm on Ubuntu linux if that matters. I have a simple project
I'm running IntelliJ Idea under linux. I have created a project and a module
I'm porting a Windows application to Linux and I have a synchronization problem. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.