Problem with Global Variable Declarations (QT)

Discussion in 'C++' started by Chris Portka, Dec 5, 2006.

  1. Chris  Portka

    Chris Portka Guest

    I have a problem I've been working on for days now with global variable
    use. I'm currently using QT for development but this same problem
    should apply to anytime a C++ object is trying to be used globally.
    Just assume the QT stuff is fine and that they are predefined library
    objects. Anyway, I want to have a global QTextStream object that I use
    to print debug information to. This program compiles fine, but if I
    ever try to use my global QTextStream object it gives me the error:
    "binary '<<' : no operator found which takes a left-hand operand of
    type 'QTextStream'"
    Any suggestions would be much appreciated. Thanks,
    Chris

    Here's all the relevant code:

    DEBUG.H:

    #ifndef DEBUG_H
    #define DEBUG_H

    #include <QMainWindow>
    #include <QLabel>
    #include <QTextStream>
    #include <QSTring>
    #include <QBuffer>

    static QTextStream debugout;

    class Debug : public QMainWindow
    {
    Q_OBJECT

    public:
    QLabel debugLabel;
    QByteArray debugArray;
    QBuffer debugBuffer;
    Debug();

    private:

    private slots:
    void textWritten(int numBytes);
    };

    #endif

    DEBUG.CPP:

    #include "Debug.h"

    Debug::Debug()
    {
    setCentralWidget(&debugLabel);
    debugout.setDevice(&debugBuffer);

    connect(&debugBuffer, SIGNAL(bytesWritten(int)), this,
    SLOT(textWritten(int)));
    }

    void Debug::textWritten(int numBytes)
    {
    QByteArray newText = debugBuffer.readAll();
    debugArray.append(newText);
    debugLabel.setText(debugArray);
    }
     
    Chris Portka, Dec 5, 2006
    #1
    1. Advertising

  2. Chris Portka wrote:

    > I have a problem I've been working on for days now with global variable
    > use. I'm currently using QT for development but this same problem
    > should apply to anytime a C++ object is trying to be used globally.
    > Just assume the QT stuff is fine and that they are predefined library
    > objects. Anyway, I want to have a global QTextStream object that I use
    > to print debug information to. This program compiles fine, but if I
    > ever try to use my global QTextStream object it gives me the error:
    > "binary '<<' : no operator found which takes a left-hand operand of
    > type 'QTextStream'"
    > Any suggestions would be much appreciated. Thanks,
    > Chris
    >
    > Here's all the relevant code:
    >
    > DEBUG.H:
    >
    > #ifndef DEBUG_H
    > #define DEBUG_H
    >
    > #include <QMainWindow>
    > #include <QLabel>
    > #include <QTextStream>
    > #include <QSTring>
    > #include <QBuffer>
    >
    > static QTextStream debugout;


    Suggestion #1) Don't do that. Look at the docs for QDebug.
    Suggestion #2) Post to Your question really is
    Qt-specific.
    Suggestion #3) Don't do that. Globals are ugly.
    Suggestion #4) http://doc.trolltech.com/4.2/qtextstream.html#setPadChar

    QString s;
    QTextStream out(&s);
    out.setFieldWidth(10);
    out.setPadChar('-');
    out << "Qt" << endl << "rocks!" << endl;


    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
     
    Steven T. Hatton, Dec 5, 2006
    #2
    1. Advertising

  3. Steven T. Hatton a écrit :
    > Chris Portka wrote:
    >
    >> I have a problem I've been working on for days now with global variable
    >> use. I'm currently using QT for development but this same problem
    >> should apply to anytime a C++ object is trying to be used globally.
    >> Just assume the QT stuff is fine and that they are predefined library
    >> objects. Anyway, I want to have a global QTextStream object that I use
    >> to print debug information to. This program compiles fine, but if I
    >> ever try to use my global QTextStream object it gives me the error:
    >> "binary '<<' : no operator found which takes a left-hand operand of
    >> type 'QTextStream'"
    >> Any suggestions would be much appreciated. Thanks,
    >> Chris
    >>
    >> Here's all the relevant code:
    >>
    >> DEBUG.H:
    >>
    >> #ifndef DEBUG_H
    >> #define DEBUG_H
    >>
    >> #include <QMainWindow>
    >> #include <QLabel>
    >> #include <QTextStream>
    >> #include <QSTring>
    >> #include <QBuffer>
    >>
    >> static QTextStream debugout;

    >
    > [...]
    > Suggestion #3) Don't do that. Globals are ugly.
    > [...]
    >
    >


    Yes, ugly. Especially declared static in a header file.

    Use static within a class to make it global to the class but otherwise,
    use extern.

    in debug.hpp:
    ....
    extern QTextStream debugout;
    ....

    in debug.cpp
    ....
    QTextStream debugout;
    ....
     
    Michael DOUBEZ, Dec 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff
    Replies:
    2
    Views:
    6,576
  2. Elliot Marks

    global struct declarations

    Elliot Marks, Jan 7, 2004, in forum: C Programming
    Replies:
    20
    Views:
    866
    Thomas Stegen
    Jan 10, 2004
  3. Replies:
    1
    Views:
    405
    Michael Ekstrand
    Aug 21, 2005
  4. Douglas
    Replies:
    2
    Views:
    364
    Herbert Rosenau
    Jul 5, 2004
  5. Alex Hall
    Replies:
    5
    Views:
    246
    Lawrence D'Oliveiro
    May 11, 2010
Loading...

Share This Page