Change some code in ui_xxx.h
-
In ui_myclass.h we can find these lines:
@namespace UI {
class MyClass : public Ui_MyClass {}
} // namespace Ui@I think we can have a better way to do this. That's my code:
@namespace UI {
typedef Ui_MyClass MyClass;
} // namespace Ui@Both of them do the same things. But the former will call one more constructor than the latter.
I don't know why qt doesn't use the latter. It's much easier. -
A forward declaration of a class (to avoid #including the header) is not possible with a typedef.
In a header:
@
class Foo {
public:
Foo() { }
~Foo() { }int foobar;
};
typedef Foo FooAlias;
@and later on in another header:
@
class FooAlias;class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
FooAlias *f;
};
@and in the implementation of the MainWindow:
@
#include "mainwindow.h"
#include "foo.h"
@If you try to compile the .cpp of the MainWindow class you get an error message like this:
@
foo.h:9: error: conflicting declaration 'typedef class Foo FooAlias'
mainwindow.h:1: error: 'struct FooAlias' has a previous declaration as 'struct FooAlias'
@ -
i test it with gcc 3.3. Forward declaration are supported.
[quote author="Volker" date="1301482447"]A forward declaration of a class (to avoid #including the header) is not possible with a typedef.
In a header:
@
class Foo {
public:
Foo() { }
~Foo() { }int foobar;
};
typedef Foo FooAlias;
@and later on in another header:
@
class FooAlias;class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
FooAlias *f;
};
@and in the implementation of the MainWindow:
@
#include "mainwindow.h"
#include "foo.h"
@If you try to compile the .cpp of the MainWindow class you get an error message like this:
@
foo.h:9: error: conflicting declaration 'typedef class Foo FooAlias'
mainwindow.h:1: error: 'struct FooAlias' has a previous declaration as 'struct FooAlias'
@[/quote] -
[quote author="ibingow" date="1303381454"]i test it with gcc 3.3. Forward declaration are supported.
[/quote]Forward declaration in general is supported, yes. But not the construct mentioned above.
You can try yourself, this is a complete example:
------ foo.h ------
@
#ifndef FOO_H
#define FOO_Hclass Foo {
public:
Foo() { }
~Foo() { }int foobar;
};
typedef Foo FooAlias;
#endif // FOO_H
@------ using.h ------
@
#ifndef USING_H
#define USING_Hclass FooAlias;
class UsingFoo
{public:
explicit UsingFoo();
~UsingFoo();private:
FooAlias *f;
};#endif // USING_H
@------ using.cpp ------
@
#include "using.h"
#include "foo.h"UsingFoo::UsingFoo()
{
f = new FooAlias;
}UsingFoo::~UsingFoo()
{}
@------ main.cpp ------
@
#include "using.h"int main(int argc, char *argv[])
{
UsingFoo uf;
return 0;
}
@Compile with
@
g++ -c -o main.o main.cpp
g++ -c -o using.o using.cpp
g++ -o typedeftest main.o using.o
@It definitely fails with gcc 4.2.x and 3.4.x, all with the very same error message I posted earlier.
-
[quote author="ibingow" date="1309876061"]Yes, you're right. But it's strange that my gcc 3.3.1 can compile your example without warning and error. In fact, in your example, we can write UsingFoo like that
[/quote]I just don't believe you. My code fails with gcc 3.2.2 with the same error messages. It is very unlikely that it failed on 3.2.x, compiled on 3.3.x and failed again on 3.4.x. You must have modified the code or tweaked some compiler switches.