PDA

View Full Version : Cannot #include some classes



Jayes
11th March 2010, 22:46
I am moving a little app I have written from QT3 to QT4 (debian 4.4.5.3). When I inlcude some classes, like QListview and QComboBox, I get compilation errors. Just the #include line is enough to get these errors, no need to actually do something with the included class. Here is what I get when I include QListview:

In file included from /usr/include/qt4/QtGui/qabstractitemdelegate.h:46,
from /usr/include/qt4/QtGui/qabstractitemview.h:48,
from /usr/include/qt4/QtGui/qlistview.h:45,
from /usr/include/qt4/QtGui/QListView:1,
from src/qm_tracks.h:41,
from src/qm_player.h:52,
from src/qm_core.h:27,
from src/main.cpp:23:
/usr/include/qt4/QtGui/qstyleoption.h:139: error: expected identifier before numeric constant
/usr/include/qt4/QtGui/qstyleoption.h:139: error: expected '}' before numeric constant
/usr/include/qt4/QtGui/qstyleoption.h:139: error: expected unqualified-id before numeric constant
/usr/include/qt4/QtGui/qstyleoption.h:142: error: 'FrameFeature' was not declared in this scope
/usr/include/qt4/QtGui/qstyleoption.h:142: error: template argument 1 is invalid
/usr/include/qt4/QtGui/qstyleoption.h:142: error: invalid type in declaration before ';' token
/usr/include/qt4/QtGui/qstyleoption.h:145: error: expected unqualified-id before ')' token
/usr/include/qt4/QtGui/qstyleoption.h:146: error: expected unqualified-id before 'const'
/usr/include/qt4/QtGui/qstyleoption.h:146: error: expected ')' before 'const'
/usr/include/qt4/QtGui/qstyleoption.h:147: error: expected unqualified-id before 'const'
/usr/include/qt4/QtGui/qstyleoption.h:147: error: expected ')' before 'const'
etc.
etc.

Perhaps a required dependency is missing? Debian should have said so...

Any ideas?

Thanks in advance.

Jayes.

Lykurg
11th March 2010, 23:04
What is the content of these files at the mentioned lines?

from src/qm_tracks.h:41,
from src/qm_player.h:52,
from src/qm_core.h:27,
from src/main.cpp:23:

Is it just the include line?

Jayes
12th March 2010, 00:20
What is the content of these files at the mentioned lines?
Is it just the include line?

Yes, that's just one file including the next. Finally qm_tracks.h includes the qt class that is acting up.

--
jayes

user_mail07
12th March 2010, 00:42
Did you have modules added in project files? In your .Pro file you would have something like this:-


QT += gui core

or you can check in your project properties if you have:-


qt\4.4.5.3\include\QtGui
qt\4.4.5.3\include\QtCore

Jayes
12th March 2010, 01:03
Did you have modules added in project files?

Yes, I have:

QT = core gui

--
jayes

user_mail07
12th March 2010, 01:08
Can you post some sample application code or example code to reproduce that problem. It has something to do with you with your project dependency. Try to just post .pro file and included header files.

norobro
12th March 2010, 04:11
Here's my WAG:

Line 139 in qstyleoption.h is:
None = 0x00,Do you have a "#define None 0" in one of the Quimup headers?

Jayes
12th March 2010, 07:22
Do you have a "#define None 0" in one of the Quimup headers?

Yes, I have the same Line 139 in qstyleoption.h

For every class that acts up the errors are always the same (in qstyleoption.h).
I searched a little more and came up with an old hack that fixes the problem:

(see http://www.voxforge.org/home/forums/message-boards/general-discussion/-german-speech-recognition-suite-gpl)

In qstyleoption.h line 112+, insert:
#ifdef None
#undef None
#endif

This does indeed take care of the problems.

What bothers me now is the fact that I have no idea what is fixed exactly... and this hack dates back to 2007! This should have been fixed by now.

--
jayes

Jayes
12th March 2010, 07:27
]Do you have a "#define None 0" in one of the Quimup headers?

Hang on... I now see what you were asking!

No, I don't have a "#define None 0" in any of my headers. And this looks like it has everything to do with the hack I described.

Is "#define None 0" required for qt4 apps?

.. and thanks for helping me out.

--
jayes

wysota
12th March 2010, 12:25
Try reordering include entries in main.cpp. Could you paste the contents of that file (the includes section actually) here?

Jayes
12th March 2010, 15:24
Try reordering include entries in main.cpp.

I doubt if that iwould make any difference. Whatever the order, when the errors occur those includes have all been included.

Nevertheless, here you are:

main.cpp


#include <QApplication>
#include "qm_core.h"

int main( int argc, char ** argv )
{
QApplication app(argc, argv);
qm_core core;
return app.exec();
}

qm_core.h

#include <QMainWindow>
#include "qm_player.h"

class qm_core : public QMainWindow
{
Q_OBJECT
public:
qm_core();
virtual ~qm_core();

private:
qm_player player;

};


qm_player.h


#include <QMainWindow>
#include <QApplication>
#include <QCloseEvent>
#include <QFrame>
#include <QLabel>
#include <QPushButton>
#include <QSlider>
#include <QImage>
#include <QPixmap>
#include <QSettings>
#include <QTimer>
#include <QSignalMapper>
#include <QPalette>
#include <QDir>
#include <QProcess>
#include <QVBoxLayout>
#include <QMenu>
#include <stdlib.h>
#include "libmpdclient.h"
#include "qm_clicklabel.h"
#include "qm_clickprogressbar.h"
#include "qm_config.h"
#include "qm_mpdcom.h"
#include "qm_trayicon.h"
#include "qm_widget_ids.h"
#include "qm_scroller.h"
#include "qm_settings.h"
#include "qm_tracks.h"

finally: qm_tracks.h


#include <QMainWindow>
#include <QApplication>
#include <QWidget>
#include <QButtonGroup>
#include <QCheckBox>
#include <QFrame>
#include <QVariant>
#include <QAction>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpacerItem>
#include <QSplitter>
#include <QList>
#include <QPixmap>
#include <QTabWidget>
#include <QTimer>
#include <QListView>
#include <QComboBox>
#include <QCloseEvent> (etc.)


PS. I intercept the CloseEvent in qm_player, which would prevent KDE from shutting down if the player was the top window. That's why I made it a sibling of the 'fake' window called qm_core.

--
jayes

wysota
12th March 2010, 17:09
I doubt if that iwould make any difference. Whatever the order, when the errors occur those includes have all been included.
It does make a difference. Consider the following example:


// file1.h
#ifndef X
#define X int
#endif

// file2.h
#ifndef X
#define X std::string
#endif
and then two orders of inclusion:

// main.cpp
#include "file1.h"
#include "file2.h"
//...
X x;

// main.cpp
#include "file2.h"
#include "file1.h"
// ...
X x;
Now in version 1 "x" will be of type "int" but in version 2 it will be of type "std::string". So the order may matter (in a place AFTER the files have been included - when "x" is declared).

Try reordering the includes and try to substitute all the includes you can with forward declarations.

Jayes
12th March 2010, 20:31
Try reordering the includes and try to substitute all the includes you can with forward declarations.

Wysota, you are not just a master of Zen...

I tried many permutations and found that, in qm_player.h

this does not workt:

#include "qm_config.h"
#include "qm_tracks.h"

and this works fine:

#include "qm_tracks.h"
#include "qm_config.h"

The culprit is

#include <X11/Xlib.h>

in qm_config.h. It is something I can do without, so I removed it. Now I can put qm_tracks.h where I please.

Well, I learned something today. Thank you very much indeed!

--
jayes

norobro
13th March 2010, 17:36
Jayes,

I didn't think that the include order of header files made any difference either. Wysota's explanation cleared that up for me. Anyway I was browsing around in the sources and found this in qcoreevent.h:
enum Type {
/*
If you get a strange compiler error on the line with None,
it's probably because you're also including X11 headers,
which #define the symbol None. Put the X11 includes after
the Qt includes to solve this problem.
*/
None = 0, // invalid event
It would have been nice if that comment was in qstyleoption.h, huh?
Norm

squidge
13th March 2010, 21:41
IMO, It's just another reason why you should never have such ambiguous symbols in your code base. I dislike code which is like this (Although I don't avoid such code unless it's small or easily rewritten). Either put the enum in a class, or prefix it with the subsystem its involved with. Eg. if "None" is associated with the event code, then it should be named "QtEvent_None".