При добавлении класса для работы с БД всплывают ошибки!
mainwindow.o: In function `DatabaseAccessor':
/home/stas/MyDB/mainwindow.cpp:33: undefined reference to `DatabaseAccessor::dbHost'
/home/stas/MyDB/mainwindow.cpp:34: undefined reference to `DatabaseAccessor::dbName'
/home/stas/MyDB/mainwindow.cpp:35: undefined reference to `DatabaseAccessor::dbUser'
/home/stas/MyDB/mainwindow.cpp:36: undefined reference to `DatabaseAccessor::dbPass'
/home/stas/MyDB/mainwindow.cpp:33: undefined reference to `DatabaseAccessor::dbHost'
/home/stas/MyDB/mainwindow.cpp:34: undefined reference to `DatabaseAccessor::dbName'
/home/stas/MyDB/mainwindow.cpp:35: undefined reference to `DatabaseAccessor::dbUser'
/home/stas/MyDB/mainwindow.cpp:36: undefined reference to `DatabaseAccessor::dbPass'
collect2: ld returned 1 exit status
make: Выход из каталога `/home/stas/MyDB'
make: *** [MyDB] Ошибка 1
Завершено с кодом 2.
Ошибка во время сборки проекта MyDB
Во время выполнения сборки на этапе 'Make'
.pro
# -------------------------------------------------
# Project created by QtCreator 2011-01-13T19:05:29
# -------------------------------------------------
QT += sql
TARGET = MyDB
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
void create_menu();
private:
Ui::MainWindow *ui;
};
class DatabaseAccessor
{
public:
static DatabaseAccessor* getInstance();
static QString dbHost;
static QString dbName;
static QString dbUser;
static QString dbPass;
private:
DatabaseAccessor();
DatabaseAccessor(const DatabaseAccessor& );
DatabaseAccessor& operator=(const DatabaseAccessor& );
QSqlDatabase db;
};
#endif // MAINWINDOW_H
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
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;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
DatabaseAccessor::DatabaseAccessor()
{
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(dbHost);
db.setDatabaseName(dbName);
db.setUserName(dbUser);
db.setPassword(dbPass);
if (db.open())
{
qDebug("connected to database");
}
else
{
qDebug("Error occured in connection to database");
}
}
DatabaseAccessor* DatabaseAccessor::getInstance()
{
static DatabaseAccessor instance;
return &instance;
}