Problem with Global Variable Declarations (QT)

C

Chris Portka

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);
}
 
S

Steven T. Hatton

Chris said:
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 (e-mail address removed) 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;
 
M

Michael DOUBEZ

Steven T. Hatton a écrit :
Chris said:
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;
....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top