# C++/Qt4 unusual error!



## youngunix (Sep 24, 2013)

_I am w_orking on a project in Qt4 that's almost finished and all of a sudden it starts throwing the following error 
	
	



```
"expected '}' at end of input"
```
in main.cpp. Here is what's inside main.cpp:

```
#include <QtGui/QApplication>
#include "mainform.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainForm w;
    w.show();

    return a.exec();
}
```

As far as I'm seeing, it shouldn't report that error when building.


----------



## fonz (Sep 24, 2013)

At first glance I see nothing wrong with your main.cpp. Have you checked mainform.h? There might be something in there that's tripping up your build.


----------



## youngunix (Sep 25, 2013)

This is odd! mainform.h looked normal, but I fixed it by changing it from:

```
#ifndef MAINFORM_H
#define MAINFORM_H

#include <QMainWindow>


namespace Ui {
    class Mclass MainForm : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainForm(QWidget *parent = 0);
    ~MainForm();
    
private slots:

    /* buttons, textboxes, etc... go here */

private:
    Ui::MainForm *ui;
};

#endif // MAINFORM_H
```
To this:

```
#ifndef MAINFORM_H
#define MAINFORM_H

#include <QMainWindow>


namespace Ui {
    class MainForm;
}

class MainForm : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainForm(QWidget *parent = 0);
    ~MainForm();
    
private slots:

    /* buttons, textboxes, etc... go here */

private:
    Ui::MainForm *ui;
};

#endif // MAINFORM_H
```


----------



## kpedersen (Sep 25, 2013)

I personally would prefer your original code because it wraps the class definition in a namespace rather than forward declaring the class (within the namespace) which means more processing by the compiler and more code for you to write. You fixed it because in the second version you added the *}*.

So in order for the original code to work use something like this:

```
#ifndef MAINFORM_H
#define MAINFORM_H

#include <QMainWindow>

namespace Ui
{

class Mclass MainForm : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainForm(QWidget *parent = 0);
    ~MainForm();
    
private slots:

    /* buttons, textboxes, etc... go here */

private:
    Ui::MainForm *ui;

};

[b][font="Impact"][SIZE="5"]}[/SIZE][/font][/b]

#endif // MAINFORM_H
```

Note the oversized *}* 

Offtopic: I am still waiting for Qt to stop being ridiculous and start using standard C++ rather than that signal/slot Moc crap. I know that in 10 years I am going to be handed some old Qt4 code to maintain and Moc is going to no longer work 
Did we not learn anything from VB6 or Borland C++ Builder?


----------



## youngunix (Sep 25, 2013)

kpedersen said:
			
		

> I personally would prefer your original code because it wraps the class definition in a namespace rather than forward declaring the class (within the namespace) which means more processing by the compiler and more code for you to write. You fixed it because in the second version you added the *}*.
> 
> So in order for the original code to work use something like this:
> 
> ...



I gave your suggestion a shot, but unfortunately it didn't fly. Thanks for the reply though.
Qt is anal about many things, and yes it is annoying and confusing at times. But for me, anything to stay away from Microsoft products.


----------



## xibo (Sep 26, 2013)

Unless you're *using namespace Ui*, w's datatype in main is Ui::MainForm, while you can omit the namespace in the private variable declaration of the MainForm-structure specification.

P.S. what is that private variable good for anyway? I thought about a singleton pattern at first, but it needs to be static for that...



			
				kpedersen said:
			
		

> Offtopic: I am still waiting for Qt to stop being ridiculous and start using standard C++ rather than that signal/slot Moc crap. I know that in 10 years I am going to be handed some old Qt4 code to maintain and Moc is going to no longer work
> Did we not learn anything from VB6 or Borland C++ Builder?


Well it could, and actually can (Qt5), but that means our base gcc isn't able to build _any_ Qt project any more.


----------



## kpa (Sep 26, 2013)

xibo said:
			
		

> Unless you're *using namespace Ui*, _w_'s datatype in _main_ is _Ui::MainForm_, while you can omit the namespace in the private variable declaration of the _MainForm_-structure specification.
> 
> ps: what is that private variable good for anyway? I thought about a singleton pattern at first, but it needs to be static for that...
> 
> ...



In future versions of FreeBSD the only C/C++ compiler in the base will be clang(1), no gcc(1) unless you install it from ports.


----------



## kpedersen (Sep 26, 2013)

Interestingly, when we do tell the ports to use gcc

```
USE_GCC=any
```
 it defaults to version 4.6.4. This seems to support a very large amount of the new C++ standard. I might actually have a play with Qt5 if it can indeed use standard C++. Cheers for letting me know


----------

