PDA

View Full Version : Weird compile error



MarkoSan
3rd December 2007, 17:43
Hello again! I need hlp pls!

I have include file globals.h and here is its contens:
// globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

// qt includes
#include <QtGlobal>

// scene rect coords
static const qint16 minX=-300;
static const qint16 minY=-300;
static const qint16 maxX=300; // scene width
static const qint16 maxY=300; // scene height

if (minX<=0) {
static const qreal rCenterX=(maxX+minX)/2; // scente centre x
} else {
static const qreal rCenterX=(maxX-minX)/2; // scente centre x
};

if (minY<=0) {
static const qreal rCenterY=(maxY+minY)/2; // scente centre x
} else {
static const qreal rCenterY=(maxY-minY)/2; // scente centre x
};

static const qreal rCirleMargin=20.0; // circle margin
static const qreal rCircleRadius=maxX-rCenterX-rCirleMargin; // radius of circle

// geometric constants
static const qreal pi=3.141592653589793238462643383279502884197169399 37510; // pi constant
static const qreal Pi=pi; // alias for pi
static const quint8 iLoadTreshold=10; // icon load treshold
static const qreal rFullCircle=360; // full circle has 360 degrees
static const qreal rAngle=rFullCircle/iLoadTreshold; // merchandize icon appears every rAngle degrees on rotary
// merchandize selector

#endif

If I include this file into project, I get following weird compiler errors:
C:\Documents and Settings\markofr\workspace\eROS>make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/markofr/workspace
/eROS'
c:\Qt\4.3.2\bin\uic.exe crosmainwindow.ui -o ui_crosmainwindow.h
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_DLL -DQT_SVG_LIB -DQT_SQL_LIB -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB
-DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.2\inclu
de\QtCore" -I"..\..\..\..\Qt\4.3.2\include\QtCore" -I"..\..\..\..\Qt\4.3.2\inclu
de\QtNetwork" -I"..\..\..\..\Qt\4.3.2\include\QtNetwork" -I"..\..\..\..\Qt\4.3.2
\include\QtGui" -I"..\..\..\..\Qt\4.3.2\include\QtGui" -I"..\..\..\..\Qt\4.3.2\i
nclude\QtOpenGL" -I"..\..\..\..\Qt\4.3.2\include\QtOpenGL" -I"..\..\..\..\Qt\4.3
.2\include\QtSql" -I"..\..\..\..\Qt\4.3.2\include\QtSql" -I"..\..\..\..\Qt\4.3.2
\include\QtSvg" -I"..\..\..\..\Qt\4.3.2\include\QtSvg" -I"..\..\..\..\Qt\4.3.2\i
nclude" -I"c:\Qt\4.3.2\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.2
\mkspecs\win32-g++" -o debug\CLens.o CLens.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
DQT_DLL -DQT_SVG_LIB -DQT_SQL_LIB -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB
-DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.2\inclu
de\QtCore" -I"..\..\..\..\Qt\4.3.2\include\QtCore" -I"..\..\..\..\Qt\4.3.2\inclu
de\QtNetwork" -I"..\..\..\..\Qt\4.3.2\include\QtNetwork" -I"..\..\..\..\Qt\4.3.2
\include\QtGui" -I"..\..\..\..\Qt\4.3.2\include\QtGui" -I"..\..\..\..\Qt\4.3.2\i
nclude\QtOpenGL" -I"..\..\..\..\Qt\4.3.2\include\QtOpenGL" -I"..\..\..\..\Qt\4.3
.2\include\QtSql" -I"..\..\..\..\Qt\4.3.2\include\QtSql" -I"..\..\..\..\Qt\4.3.2
\include\QtSvg" -I"..\..\..\..\Qt\4.3.2\include\QtSvg" -I"..\..\..\..\Qt\4.3.2\i
nclude" -I"c:\Qt\4.3.2\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.2
\mkspecs\win32-g++" -o debug\cdatabasefoundation.o cdatabasefoundation.cpp
In file included from cdatabasefoundation.h:7,
from cdatabasefoundation.cpp:1:
globals.h:15: error: expected unqualified-id before "if"
globals.h:15: error: expected `,' or `;' before "if"
globals.h:17: error: expected unqualified-id before "else"
globals.h:17: error: expected `,' or `;' before "else"
globals.h:21: error: expected unqualified-id before "if"
globals.h:21: error: expected `,' or `;' before "if"
globals.h:23: error: expected unqualified-id before "else"
globals.h:23: error: expected `,' or `;' before "else"
globals.h:28: error: `rCenterX' was not declared in this scope
cdatabasefoundation.cpp:38:2: warning: no newline at end of file
mingw32-make[1]: *** [debug\cdatabasefoundation.o] Error 1
mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/
eROS'
mingw32-make: *** [debug] Error 2

Wtf????

jacek
3rd December 2007, 17:46
You can't put any statements outside functions. If you really need this, use preprocessor directives and macros instead.

Edit: Actually you don't need those if statements. The formula for the center is: center = (max + min) / 2, regardless if min is positive or not.

DeepDiver
3rd December 2007, 17:47
the error is pretty clear: rCenterX is not decared in the scope

explanation:
you have declared rCenterX inside the brakes of the if statement. it's only valid inside .

DeepDiver
3rd December 2007, 17:48
You can't put any statements outside functions.

Yes - and that as well ;-)

MarkoSan
3rd December 2007, 19:04
Ok, I see, sorry for stupid question. I am a little rusty at c++. Can you:
1) Please move this thread to c++, siince it has no connection with qt
2) How can I make macro, that will compute CenterX and CenterY?

marcel
3rd December 2007, 19:13
2)


#define CENTER(x1, x2) ((x1+x2)/2)
That should do it.
You can pass x or (-x) for the second param.

OR, better yet:


#define CENTER(x1,x2,result) {if (x2<=0){result=(x1+x2)/2;}else{result=(x1-x2)/2;}}
This one should be easier to use.

jacek
3rd December 2007, 19:50
How can I make macro, that will compute CenterX and CenterY?
First of all your original formula is wrong --- you don't need the if statement.

When I have mentioned the macros, I wanted to say that if you want to have an if statement outside any function, you have to use #if directive instead. Since it doesn't work with variables, you need macros too.

The problem is that macros are Bad. If you want something that calculates CenterX and CenterY, use inline functions.