Can't declare QPixmap as global variable
-
While trying to create a QPixmap as a global variable, i'm getting the following LINK errors:
•main.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)
•mainwindow.obj:-1: error: LNK2001: unresolved external symbol "class QPixmap * _corner" (?_corner@@3PEAVQPixmap@@EA)Here's MainWindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtGui> #include <QtCore> #include <QGraphicsDropShadowEffect> extern QPixmap* _corner; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; protected: void paintEvent(QPaintEvent *e); }; #endif // MAINWINDOW_H
and main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; _corner->load("C:\\Users\\Domenico\\Desktop\\corner.png"); w.show(); return a.exec(); }
-
This seems a misunderstanding of language basics.
Global and extern meaning are different.I would highly advice reading the book.
But briefly:
In such context extern means that you want to access variable _corner which is declared and instantiated in other module.
Since it is not true you get a link error.At the same time I would highly advice against global variables in C++.
Using such leads to code which is highly difficult to maintain.
If you really need something accessible globally you may either subclass QApplication or use singleton pattern
or at least use static class members .