I'm having a static linking problem in my C++ app. I'm hoping you can help. Code for header and source below.
#ifndef PRACTICARDSDB_H
#define PRACTICARDSDB_H
#include "cardset.h"
#include "card.h"
#include "filter.h"
class PractiCardsDB
{
public:
PractiCardsDB();
static void resetAll();
static void resetDates();
static CardSet getCardSet();
static CardSet getCardSet(Filter filter);
static void addCard(Card card);
static void editCard(Card card);
static void deleteCard(Card card);
static bool createConnection();
};
#endif // PRACTICARDSDB_H
Above is the header file and below is the source file.
#include "practicardsdb.h"
#include <QtSql/QSqlDatabase>
#include <QMessageBox>
PractiC开发者_运维问答ardsDB::PractiCardsDB() {}
static bool PractiCardsDB::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("EnglishSpanish");
if (!db.open())
{
return false;
}
return true;
}
The error I receive is: cannot declare member function 'static bool PractiCardsDB::createConnection()' to have static linkage. Any help?
I'm using Qt 4.7 with C++ inside Qt Creator if it helps.
When you define a static
member function separately from the declaration, you do not have to use the static
modifier.
bool PractiCardsDB::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("EnglishSpanish");
if (!db.open())
{
return false;
}
return true;
}
Also do you really mean to make every single function of your class static
? Your class represents a database of sorts for Card
objects, so I think you'd want to actually store member data with the class itself?
Even in that snippet above, you create a QSqlDatabase
object, but db
's existence is only the extent of the createConnection()
function.
Remove static
decleration from your cpp
file, it should only be in the header
file. Like:
bool PractiCardsDB::createConnection()
{
....
}
精彩评论