How to declare global variable in QT?
-
So did you include deb.h where you want to use x? Also are you sure you don't have a function named x() in this class?
And please avoid global variables - they are not needed in most cases. -
@Christian-Ehrlicher Thank you for answer. In last post I paste all my deb class code.
Here is my mainWindow code:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "deb.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); qDebug()<<x; } MainWindow::~MainWindow() { delete ui; }
I don't have other x function or variable.
-
@TomNow99 said in How to declare global variable in QT?:
I don't have other x function or variable.
You have: https://doc.qt.io/qt-5/qwidget.html#x-prop
Use
::x
- but it's c++ basics I would guess and has nothing to do with Qt :) -
@Christian-Ehrlicher It was.... Wow :D Thank you very much! Yes, x and y is position :D Thank you!
-
@TomNow99
If in yourdeb.h
file you show you haveint x=4;
in addition toextern int x;
, then at least when you include that into multiple.cpp
files you will end up with "multiple definitions ofx
variable" when you link. Definitions/initializations of "global" variables belong in one.cpp
file, not in a.h
file. EDIT Ah perhaps you are already doing this, I can't figure where what you show is situated.OK, I see what is @Christian-Ehrlicher is spotting. One obvious conclusion: if you are going to have "global" variables, don't name them anything like
x
!! -
@JonB @Christian-Ehrlicher I have one more question.
I need only one thing to have everything, what I would like.
When I look at std::cout I can use it everywhere ( of course when I include iostream library ), but I can't create next std::ostream object ( because of protected constructor ). I would like the same in my project ( can't create other than that global object ).
But when I change public to private constructor of my class, I will not create object in deb.cpp ( here I would like to create my object. Only here! ). I know that I can create some function singleton, but I would like that trick, which is in iostream and cout.
So I would like have:
#ifndef DEB_H #define DEB_H class deb { private: deb(); }; extern deb xx; #endif // DEB_H
and
#include "deb.h" deb xx; // error - constructor is private deb::deb() { }
-
You mix something up - std::cout is no std::ostream - it's a global object which takes a std::ostream via the
<<
operator.
std::ostream is a base class of the different output possibilities like iostream or ofstream. -
@Christian-Ehrlicher Here:
https://en.cppreference.com/w/cpp/io/cout
I see ( on the top ):
extern std::ostream cout;
EDIT:
So I tried create std::ostream cout2; and I can'tEDIT2:
So cout is object of class... ? Which one?