PDA

View Full Version : Problem with QFlags and macro Q_DECLARE_FLAGS - typedef cannot be seen.



miqqo
7th July 2010, 21:50
Hi everyone, is there anybody who can help me with follwoing problem? I would like to use QFlags class. When compiling below code i get error.


#ifndef OBJECTQ_H
#define OBJECTQ_H

#include <QtCore/qglobal.h>
#include <QtCore/qobject.h>

class Objectq : public QObject
{
public:
enum MyEnum
{
Enum1 = 0x0,
Enum2 = 0x1
};
Q_DECLARE_FLAGS(MyEnums, MyEnum)
Objectq();
MyEnums flags() const;

private:
MyEnums mFlags;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Objectq::MyEnums);

#endif // OBJECTQ_H


The compiler output is following:


..\Flagi\objectq.cpp(7) : error C2143: syntax error : missing ';' before 'Objectq::flags'
..\Flagi\objectq.cpp(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
..\Flagi\objectq.cpp(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
..\Flagi\objectq.cpp(8) : error C2556: 'int Objectq::flags(void) const' : overloaded function differs only by return type from 'Objectq::MyEnums Objectq::flags(void) const'
c:\users\lukasz\workspace\flagi\objectq.h(17) : see declaration of 'Objectq::flags'
..\Flagi\objectq.cpp(8) : error C2371: 'Objectq::flags' : redefinition; different basic types
c:\users\lukasz\workspace\flagi\objectq.h(17) : see declaration of 'Objectq::flags'


It seems that in method MyEnums flags() const; type MyEnums isn't visible, but I don't have idea why. According to QT documentation macro Q_DECLARE_FLAGS should do the job, but it isn't. I've spent a lot of time trying to get known what's is wrong, now it's time to ask for help somone clever than me ;).

tbscope
8th July 2010, 05:47
The errors you posted are from the cpp file, not the header file.
Please post the code of the cpp file.

And please read the error messages from top to bottom. The most obvious problem (and this will reduce a lot of errors) is the missing ; at line 7

miqqo
8th July 2010, 16:03
I doubt that it is a matter a cpp file, it is very simple test class only:

objectq.cpp:


#include "objectq.h"

Objectq::Objectq()
{
}

MyEnums Objectq::flags() const
{
return mFlags;
}
#include "objectq.h"

Objectq::Objectq()
{
}

MyEnums Objectq::flags() const
{
return mFlags;
}

tbscope
8th July 2010, 16:57
Use


Objectq::MyEnums

everywhere instead of just MyEnums.

jkyle
18th July 2010, 04:20
Don't you need to have the Q_OBJECT macro first?

tbscope
18th July 2010, 08:39
Don't you need to have the Q_OBJECT macro first?

No, flags can also be used in non QObject based classes. See the example in the QFlags documentation.