1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Single ton class operator

Discussion in 'C++' started by creative, May 31, 2010.

  1. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have the following problem:

    I have a singleton of a class and this class has just NEN <<operator. But while compiling I get the following error:

    Code:
    error C2296: '<<' : illegal, left operand has type 'class CLog *'
    error C2297: '<<' : illegal, right operand has type 'char [5]'
    
    Here's the code:
    Code:
    // CLog.h
    class CLog
    {
    public:
        static CLog      *getInstance   (void) { return (&m_Instance); }
    
        void setFile (const char *FileName = "Protokoll.htm");
    
        CLog& operator<< (void* n);
    
    private:
        CLog (void);
    
        static  CLog    m_Instance;
    
        std::ofstream   m_Stream;
    }; 
    
    the cpp:
    Code:
    #include "CLog.h"
    
    CLog CLog::m_Instance;
    
    CLog::CLog(void)
    {
    }
    
    CLog& CLog::operator<< ( void* n )
    {    
        this->m_Stream << n;
    
        return (*this);
    } 
    
    and here the main.cpp:
    Code:
    #include "CLog.h"
    
    int main (void)
    {
        CLog            *p_Protokoll;
        p_Protokoll     = CLog::getInstance ();
    
        p_Protokoll << "Test" << std::endl;
    
        return(false);
    }
    
    I have to rebuild the Singleton or how I get the <<operators, to the run it? : Confused:
     
  2. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    you work with in the refernzen or dereferenzierst pointer

    Code:
    int main (void)
    {
        CLog            *p_Protokoll;
        p_Protokoll     = CLog::getInstance ();
    
        *p_Protokoll << "Test" << std::endl;
    
        return(false);
    }  
    
    ahja
    Code:
    CLog            *p_Protokoll;
        p_Protokoll     = CLog::getInstance (); 
    
    Although this rule does not apply when buldins, the link is preventive

    false becomes a convertiert and returned in the return-value of main means anything other than 0, but an error
     
  3. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    How can you ahja std:: endl stream in your clogs?
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the info. would put on the change, oh yeah, this is the pointer just fine, but no gibbet "elegant" solution?

    For write this is extremely ugly at the beginning:
    Code:
    *p_Protokoll << "Test" << "test2";	
    
    Is there the possibility that some things done this:
    Code:
    Protokoll << "Test" << "Test2";	
    
     
  5. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    CLog & Protokoll = *CLog::getInstance();
    
    I would also modify the type of return of getInstanz from pointer to reference
     
  6. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Sure!
    Code:
    CLog &Protokoll = *CLog::getInstance();
    
    Protokoll << "muhkuh"; 
    
     
  7. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Hm, so I've now made the getInstance Ner reference, but there are still errors.
    Code:
    static CLog      &getInstance   (void) { return (m_Instance); }
    
    and I get on the line here:
    Code:
    CLog &Protokoll = *CLog::getInstance();
    
    this error:
    Code:
    error C2100: illegal indirection	
    
     
  8. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    If you give back ne reference do not need to dereference (leave out the *)
     
  9. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    sorry but your operator <<is the biggest waste. Look at you n little in STL Source to take next, or the best book on hand.
     
  10. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    if I write:
    Code:
    CLog &Protokoll = *CLog::getInstance();
    
    smears from the program
     
  11. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Yes, because you dereferenzierst. You shall leave out the *
     
  12. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    if I omit it compiles without error, but when running it from a functional smears
     
  13. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    habs got done ", thx for the help:)

    @ LasT:
    such as you would <The operator <do? : Confused:
     
  14. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    template<class T>
    CLog& operator<<(T& t)
    {
    m_Stream << t; 
    
    Your probably not even work yet
     

Share This Page