PDA

View Full Version : "not used" warning message for something that _is_ used



szisziszilvi
31st March 2011, 13:39
Hi,

my testprogram works fine, I just wonder why I keep having a warning message.

I created a new GUI project, and gave one single more header file, which was "stolen" from the chatchedtable exapmle, but I modified the contents of the createconnection file in order to make connection to my own database. So now it looks like this:

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QtSql/QSqlDatabase>

static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("myhostname");
db.setPort(5432);
db.setDatabaseName("mydbname");
db.setUserName("myusn");
db.setPassword("mypwd");

if (!db.open()) return false;
else return true;
}
#endif // CONNECTION_H

I included this header to the mainwindow.h with the line
#include "createconnection.h"
and there's a simple QSqlTableModel output of a table of my database just to see if it works.

And that's all. Whenever I compile the project there is the warning message:
'bool createConnection()' defined but not used
and I get it twice as if it was detected twice.

It is pretty wierd as if it wasn't used, the connection shouldn't be estabilished, should it?

wysota
31st March 2011, 15:57
How exactly do you use it? You're using MSVC, right?

szisziszilvi
2nd April 2011, 11:07
well, hard question for an "authodidactic self-encouraged non-student", but I would say no. Or I gues if I have only the Qt environment installed for programming purposes, and I use the Qt Creator then the answer is no. So the most proper answer I can give is that I use the Creator for programming (on XP), and if there is anything installed from the MS things, it must have come with the Qt installer.

wysota
2nd April 2011, 12:00
Tell us how you're using this code. Or better yet attach sources for your complete project to your post.

szisziszilvi
4th April 2011, 08:53
captured. I wanted to make a "pure code" for you, and I deleded some unused lines. As I said I copied the connection header from an in-built example, and I did not really care the included things even if I deleted the lines that would use them. So making the project pure meant also deleting the unnecessary include lines in the connection.h, and the error message misteriously disappeared. However I attach the project to this comment because I would be still interested in the answer why this happens, although this might not be a qt-related topic from this point but rather some programming issue.

MarekR22
4th April 2011, 14:14
the problem is keyword static and way you organized your code.
In this context keyword static mean: this symbol should be visible only in current unit (usually cpp file) and not shared between units.
Result is that you are defining this symbol again every time you are including this header file. In some source files you are including this header file but you are not using this symbol in this source file (this is why you have a warning).

You should do it like that:
In header file:

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QtSql/QSqlDatabase>

bool createConnection(); // declaration of method
#endif // CONNECTION_H

and in respective cpp file:

#include <yourHeaderFile.h>

// definition of symbol
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("myhostname");
db.setPort(5432);
db.setDatabaseName("mydbname");
db.setUserName("myusn");
db.setPassword("mypwd");

if (!db.open()) return false;
else return true;
}

szisziszilvi
5th April 2011, 09:14
But I include the header only once: in the mainwindow.h.

Your version works fine, but I'm still confused a bit as in this case stricly speaking the line
#include "myconnection.h"
appears twice, once in mainwindow.h and once in createconnection.cpp.

MarekR22
5th April 2011, 10:25
But I include the header only once: in the mainwindow.h.
Remember that only cpp files are compiled directly! Header files are compiled only when they are included to source file (cpp), directly or by other header file, so they may be compiled more then once.
Your header may appeared only once in mainwindow.h but this mainwindow.h appeared many times in some source files (probably in mainwindow.cpp and main.cpp - maybe in other cases too I didn't check your full code).

szisziszilvi
5th April 2011, 12:19
ahha! :)
thanks!