PDA

View Full Version : error: expected unqualified-id before numeric constant



Pragya
24th May 2007, 08:08
I am facing a strange problem, the following code was running successfully on Windows but when I tried to compile the same in LINUX

QKeyEvent *e=new QKeyEvent(QEvent::KeyPress,65,Qt::NoModifier,QStri ng("> "));

I end up with the following error :( :

error: expected unqualified-id before numeric constant

Though I tried including
#include <QtCore/QEvent>
#include <QtGui/QKeyEvent>

It didn't work.

Any Suggestions are welcome.
Thanks a ton:)

marcel
24th May 2007, 08:23
QKeyEvent ( Type type, int key, Qt::KeyboardModifiers modifiers, const QString & text = QString(), bool autorep = false, ushort count = 1 )

The third parameter is a reference to a QString.
You were passing a reference to a temporary object. GCC does not allow this.

Use:


QString s = ">";
QKeyEvent *e=new QKeyEvent(QEvent::KeyPress,65,Qt::NoModifier, s );
Regards

wysota
24th May 2007, 09:09
Are you sure Marcel? My gcc (4.1.2) allows it just fine:

#include <QString>
#include <QtDebug>

void func(const QString &s){
qDebug() << s;
}

int main(){
func(QString("testing"));
return 0;
}

Try casting Qt::NoModifier to Qt::KeyboardModifiers. Using Qt::Key_Greater instead of 65 is advised as well.

marcel
24th May 2007, 09:20
OK. It works with const temp objects because the compiler is sure they won't be modified inside the function.
I'm using:


Target: i686-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5363.obj~28/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=powerpc-apple-darwin8 --with-arch=nocona --with-tune=generic --program-prefix= --host=i686-apple-darwin8 --target=i686-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5363)


Try removing the const.

Regards

wysota
24th May 2007, 09:46
Obviously if you remove "const" it won't work :)

Pragya
24th May 2007, 09:49
Hi marcel,
I tried your code but it didn't work:(

marcel
24th May 2007, 09:53
Good!
Try Wysota's now. :)

wysota
24th May 2007, 09:56
It probably won't work either :) I didn't test it, I was just guessing, but go ahead and take a shot at it.

marcel
24th May 2007, 19:02
Obviously if you remove "const" it won't work :)
Yes, but my point was that on Win ( with the ms compiler ) does work.:)

Regards

Pragya
25th May 2007, 11:41
Hi Marcel,
Could you find the solution for this?

marcel
25th May 2007, 11:47
What else have you included?
Do you get similar errors in other parts of the code?

Try including <QKeyEvent>, or event <QtGui>, to see if it works.

Regards

Pragya
28th May 2007, 11:07
Ya I tried incluing that, but it didn't work

jpn
28th May 2007, 11:33
Is the space in the middle of "QStri ng" just a copy paste problem? Could attach the whole file or something?

Pragya
28th May 2007, 12:21
No that is not the problem.

Pragya
28th May 2007, 13:09
Hi all,
Was my question absurd :o ?
I am unable to find the clue for this, this is a big issue in my application. The same was runnig fine on Windows but unable to compile on LINUX:(

wysota
28th May 2007, 13:20
Could we see the exact and complete (including file name and line numbers) error message?

Pragya
28th May 2007, 13:31
MDIWindow2D.cxx:1665: error: expected unqualified-id before numeric constant

marcel
28th May 2007, 13:50
Can you post the entire code section?
Let's say lines 1660 - 1680?

marcel
28th May 2007, 16:38
Hey, I think I found something.
Take a look at this article: http://braincore.blogspot.com/2005_11_01_archive.html

I'm talking about:


cpp: preprocessing can bite

Ready for a new type of post? cpp? What is it? Well, KDE and Qt are primarily written in C++. I do have experience in that language. And sometimes a question pops up that begs for an answer.So here goes.
qnamespace.h:833: error: expected identifier before numeric constant
qnamespace.h:833: error: expected unqualified-id before numeric constant
Say what? Pretty cryptic error message. So, what's happening in qnamespace.h at that particular line?// documented in qcursor.cpp
enum CursorShape {
ArrowCursor,
UpArrowCursor,
CrossCursor,
....
Line 833 is the one that starts with the enum. At this point, the problem still evaded me. So, let's have a look at the source that is compiled. It contained among others these lines#include "config.h" // HAVE_LIBXSS

#ifdef HAVE_LIBXSS // Idle detection.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>
#include <fixx11h.h>
#endif // HAVE_LIBXSS

#include <kapplication.h>Well, after undefining HAVE_LIBXSS after the inclusion of the config header, the error went away. Hmmm. Could it be that somewhere in the X11 headers the name CursorShape is used? So, fire up a konsole, navigate to the directory holding the relevant headers and execute find . -type f | xargs grep CursorShapewhich turns up#define CursorShape 0 /* largest size that can be displayed */Aha! The plot thickens. Because when this is preprocessed before the enum in qnamespace.h, that particular enum will come out like enum 0 {... which is obviously not a good thing.This is a classic case of how preprocessing can bite a programmer and yield an error message that seems totally unrelated to what is actually wrong. So, the solution is to change the order in which headers are included. In this particular case that meant moving the X11 headers to the end of the #include stanzas.
Now, before you start falling all over me that this is not a solution, yes, I know it is not a proper solution. I'm aware of the fact that if one uses CursorShape after having included the X11 headers, I will get exactly the same trouble. In this case, that didn't happen. And for now, the code works.
Update -- I should have known. No, this piece is not about how to fix it, it is about how preprocessing can bite you. And yes - #undef CursorShape would be an alternate approach but equally bad. And no - including fixx11.h didn't.


I'm not saying that you have exactly the same problem but it's worth investigating further.

Judging from what I've read about this error, it appears because there is an already processed macro with the same name of a symbol you're using in your file.

So it would be better to post some code, maybe someone spots the error.

Regards

Pragya
29th May 2007, 05:54
Hi marcel,
It is not possible to put the whole code.

void MyTextBox::ProcessText()
{
char command[]="Line",num[10];
int pnts[20];
int k=0,j=0;

for(int i=0;i<str.length();i++)
{ QChar ch=this->str.at(i);
if(i<4)
command[i]=ch.toAscii();

if(isdigit(ch.toAscii()))
num[j++]=ch.toAscii();

if( i>4 && (ch == Qt::Key_Comma || ch==Qt::Key_Space ) )
{ num[j]=0;
pnts[k++]=atoi(num);
j=0;
}
}
num[j]=0;
pnts[k]=atoi(num);



QString s="";
s.append(command);

QPoint P[]={ QPoint(pnts[0],pnts[1]), QPoint(pnts[2],pnts[3]) };

if(s=="Line")
myView->DrawLine(P[0],P[1]);
else if(s=="Circ")
myView->DrawCircle(P[0],pnts[2],pnts[3]);
else if(s=="Bezi")
{ if((k+1)%2==0)
myView->DrawBezier(pnts,k+1);
else
{
QMessageBox *p=new QMessageBox(this);
p->setText("Invalid Input, Check Help");
p->setIcon(QMessageBox::Information);
p->show();
}
}
else
{
QMessageBox *p=new QMessageBox();
p->setText("No such command exist, Check Help");
p->setIcon(QMessageBox::Information);
p->show();
}

str="";
prevstr="";
QKeyEvent *e=new QKeyEvent(QEvent::KeyPress,65,Qt::NoModifier,QStri ng("> "));
keyPressEvent(e);



}

where "MyTextBox" is inherited from QTextBox

marcel
29th May 2007, 07:07
And are you positive that the error is from that line, where you create the QKeyEvent?

And what about this?

QKeyEvent *e=new QKeyEvent(QEvent::KeyPress,65,Qt::NoModifier,QStri ng("> "));

There is still a space in the middle of QString.
Jpn also asked this, and now you have the same error? How is this a copy-paste error?

Regards

marcel
29th May 2007, 10:26
I still cannot spot the error.
Does it compile if you remove that line?

Apart from the problem you have, this is not correct:


QKeyEvent *e=new QKeyEvent(QEvent::KeyPress,65,Qt::NoModifier,QStri ng("> "));
keyPressEvent(e);


keyPressEvent is not meant to be called like that.
Use qApp->postEvent instead.

Regards

Pragya
30th May 2007, 07:30
Hi Marcel,
Could you get some clue? I have traced that due to QEvent::KeyPress, the error is coming, Compiler is unable to recognize the the keyword.

marcel
30th May 2007, 07:47
Could you post the include section from that file?
All the includes.

Regards

yoblys
22nd September 2008, 13:06
Hi,

I'm using Qt4.1, and I had the same problem and I have resolved it with an include and a replacement :
#include <QKeyEvent> //that's very important
(QEvent::Type)6 instead of QEvent::KeyPress

example :
KeyEvent* kevent = new QKeyEvent((QEvent::Type)6,buffer[0],(Qt::KeyboardModifiers)Qt::NoModifier,buffer,fals e,1);

I hope it will help someone.

jpn
22nd September 2008, 13:39
You sure don't have to use such ugly C-style casts when you have the necessary include directive in place.



#include <QKeyEvent>
...
QKeyEvent* kevent = new QKeyEvent(QEvent::KeyPress,buffer[0],Qt::NoModifier,buffer,false,1);

spirit
22nd September 2008, 13:50
I don't understand why you don't use enum Qt::Key for second parameter in QKeyEvent ctor? :confused:

shammi
9th February 2020, 00:13
The problem is likely that you are including X.h which has


// X.11.h
...
...
#define KeyPress 2

So the preprocessor is converting this to...


QKeyEvent* kevent = new QKeyEvent(QEvent::2,buffer[0],Qt::NoModifier,buffer,false,1);

Try adding


#undef KeyPress // safe as long as you are not referencing KeyPress from X.h after this line
QKeyEvent* kevent = new QKeyEvent(QEvent::KeyPress,buffer[0],Qt::NoModifier,buffer,false,1);


i.e. assuming that further down you won't be referring to KeyPress from X.h

d_stranz
9th February 2020, 20:39
Why are you replying to a nearly 12 year old thread? Please check the date of the last post in a thread before bringing one back from the dead.