Can we define static class member inside constructor? [duplicate] - c++

This question already has answers here:
Why can't I initialize my static data member in my constructor
(5 answers)
Closed 6 years ago.
Say I have a static member defined in header file like:
static QHash<QString,int> flagColorsMap;
How can I define the hashmap inside the constructor that is in the cpp file. If I do the following:
Classname::flagColorsMap.insert("xyz",22);
it gives compile error :: undefined reference to Classname::flagColorMap.

In .cpp file put this line:
QHash<QString,int> Classname::flagColorsMap;
Edited:
Please check this really simple example of static QHash member initialization: create new Qt Quick project and edit MainWindow class:
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
static QHash<QString,int> flagColorsMap;
};
#endif // MAINWINDOW_H
And source file:
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
QHash<QString,int> MainWindow::flagColorsMap;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
MainWindow::flagColorsMap.insert("xyz",22);
}
MainWindow::~MainWindow()
{
qDebug() << "Value=" << flagColorsMap.value("xyz");
delete ui;
}
Is it still does not work?

I think you got this compile error because you are just "delcare" flagColorsMap but not "define" it.
So define flagColorsMap in the .cpp file maybe this error will disappeared.

Related

Using object directly from another class c++

Hi i have 3 classes that i want to use. but i dont want to create object of one class more than once. I directly want to use the object (in third class) of one class declared and initialized in second class.
To understand the problem please focus on NetworkConnection members and class defined in example below.
Class Mainwindow header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "secondscreen.h"
#include "networkconnection.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
NetworkConnection *NetworkConnectionObject;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
SecondScreen* SecondScreenObject;
};
#endif // MAINWINDOW_H
Class Main Window cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include "networkconnection.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
NetworkConnectionObject = new NetworkConnection();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int Error = NetworkConnectionObject->Connect(Username,Password);
///////////////
// This Works
//////////////
NetworkConnectionObject->LogInToken = "";
}
Class NetworkConnection Header
#ifndef NETWORKCONNECTION_H
#define NETWORKCONNECTION_H
#include <QString>
class NetworkConnection
{
public:
NetworkConnection();
int Connect(QString Username, QString Passwd);
QString LogInToken;
};
#endif // NETWORKCONNECTION_H
Now i want to use Networkclassobject directly in SeconScreenclass so that i can access LogInToken Member of MainWindowInstance.
#include "secondscreen.h"
#include "ui_secondscreen.h"
#include "mainwindow.cpp"
SecondScreen::SecondScreen(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecondScreen)
{
ui->setupUi(this);
///////////////
// This doesnot work
//////////////
MainWindow::NetworkConnectionObject->LogInToken = "";
}
SecondScreen::~SecondScreen()
{
delete ui;
}
However, when i try this compiler says
Invalid use of non-static data member "MainWindow::NetworkConnectionObject" Problem is i dont want to declare it static. Is there any way to do it.
Without seeing secondscreen.h I can't say for sure but MainWindow::NetworkConnectionObject" doesn't reference a specific instance of MainWindow class, you must reference a specific instance to modify.
If in your SecondScreen header you have something like
class SecondScreen {
// . . .
MainWindow window;
// . . .
}
then in your constructor for SecondScreen you must use the initialized instance of MainWindow, i.e.
SecondScreen::SecondScreen(QWidget* parent) : QDialog(parent) {
window.NetworkConnectionObject->LogInToken = "";
}

How is this Qt boilerplate constructor not recursive?

So when you create a standard Qt5 widget application, this is the boilerplate code for the QMainWindow subclass:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
So the class has an instance of itself, that seems fine. But then that instance itself would have an instance of itself, which in turn would have an instance of itself...
How does this not lead to an infinite recursion of classes containing themselves?
These are not the same classes. MainWindow is declared in the global namespace, while the ui member is of type Ui::MainWindow, which is declared in the Ui namespace. You can see the declaration of this class by looking at ui_mainwindow.h.
Since these are two different types, no recursion will happen, since MainWindow::~MainWindow() will not be called from within itself.

undefined-reference Error when defining a function in the *.cpp instead of in the *.h [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I have got a problem: I have 2 classes: mainwindow and ErgebnisAusFortran which look like this:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QString>
#include "ErgbnisAusFortran.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
ErgbnisAusFortran Berechnung();
ErgbnisAusFortran Berechnung_1()
{
ErgbnisAusFortran ret;
qDebug() << " ich berechne Berechnung_1..." ;
return ret;
}
private slots:
void on_pb_Calculate_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ErgbnisAusFortran.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pb_Calculate_clicked()
{
ErgbnisAusFortran Ergebnis_1;
ErgbnisAusFortran Ergebnis;
Ergebnis_1 = Berechnung_1();
Ergebnis = Berechnung();
}
ErgbnisAusFortran Berechnung()
{
ErgbnisAusFortran ret;
qDebug() << " ich berechne..." ;
return ret;
}
The thing that puzzles me is the following:
I have 2 methods Berechnung() and Berechnung_1().
Berechnung() is declared in mainwindow.h and defined in mainwindow.cpp
Berechnung_1() is declared in mainwindow.h and defined in mainwindow.h
When I run the program i get the following error concerning Berechnung():
undefined reference to MainWindow::Berechnung().Berechnung_1 works well. This puzzles me because I include mainwindow.h in mainwindow.cpp.
Does anybody know what is wrong?
thank you
itelly
You forgot to qualify the name of the member function:
ErgbnisAusFortran MainWindow::Berechnung()
^^^^^^^^^^^^
so instead this declared a new non-member function, leaving the member function undefined.

Access Qt ui from another class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am trying to make a class in Qt that will change things on my MainWindow.
This is my code so far:
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
message_function.h (My Class header)
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
namespace Ui { class MainWindow; }
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp (My Class .cpp)
#include "message_function.h"
void message_function::Print(Ui::MainWindow *ui)
{
ui->label->setText("This is a test");
}
When i buld my project i get this error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall message_function::Print(class Ui::MainWindow *)" (?Print#message_function##QAEXPAVMainWindow#Ui###Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QAE#PAVQWidget###Z)
I don't know if i am doing this correctly or if i am doing something wrong.
Can somebody help me get this to work?
Sailordi, the error is in the way you're including the header files.
I did some modifications that should fix it. However I'd like to tell you that what you're doing is not a good programming practice.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
message_function.h
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp
#include "message_function.h"
#include "ui_mainwindow.h"
void message_function::Print(Ui::MainWindow* ui)
{
ui->label->setText("This is a test");
}
You shouldn't really be trying to access the MainWindow UI from another class, a better solution is to create a signal in your class that you want to make the change from and a slot in MainWindow that executes the change.

How do I declare objects from my main window in Qt Creator?

This question should hopefully be easy to answer. I created a few buttons in my MainWindow using Qt Creator, and when I go to write the functions for the buttons, the compiler says they were not declared in this scope. What do I need to #include for these objects to be declared? The compiler error for the following would be 'baseDir' was not declared in this scope. baseDir is the objectName for a lineEdit in my window.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void getDir();
void createProj();
private slots:
void on_findDir_clicked();
void on_create_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/QApplication>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_findDir_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null );
baseDir->setText( path );
}
The items you define in your .ui file are not added directly to your main window class, they are added to its ui membre.
Try with:
ui->baseDir->setText( path );
Look at the ui_mainwindow.h file that is generated during the build if you're curious.