PDA

View Full Version : Example use namespace



giorgik
19th June 2012, 17:19
Hello everyone, I was studying the use of namespaces and tried to take a simple example, which in part works but the process is suspended, not closed.
First things first, this is my example:
geometria.h


#ifndef GEOMETRIA_H
#define GEOMETRIA_H

namespace Geometria {
float Norma(int i) {
float risultato = (float)i/6;
return risultato;
}
}

#endif // GEOMETRIA_H

main.cpp


#include <QCoreApplication>
#include <iostream>
#include "geometria.h"

using namespace std;
using namespace Geometria;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

float ris = Norma(3);
cout << "Il risultato è: " << ris << endl;

return a.exec();
}

When I launch the program, I see the result in Qt-Creator 2.5.0 but the program does not close, I have to do it by CTRL + ALT + DEL.
Can you tell me where I went wrong?

Lesiok
19th June 2012, 17:54
In this simple example You don't need a QCoreApplication instance. Just remove lines 10 and 15.

giorgik
19th June 2012, 19:56
Hi Lesiok, then I have to write code that is


#include <QCoreApplication>
#include <iostream>
#include "geometria.h"

using namespace std;
using namespace Geometria;

int main(int argc, char *argv[])
{
float ris = Norma(3);
cout << "Il risultato è: " << ris << endl;

return 0;
}

However, doing so by booting from dos shell prompt does not display anything I.

how do I do?

Added after 51 minutes:

The really strange thing is that using the following code to main.cpp (geometria.h and leaving unchanged):


#include <QCoreApplication>
#include <iostream>
#include "geometria.h"

using namespace std;
using namespace Geometria;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

float ris = Norma(3);
cout << "Il valore e': " << ris << endl;

return a.exec();
}

using the wizard to create a console project in Qt, so it works perfectly, but if I create a project wizard to empty and fill it with files main.cpp and geometria.h seen above does not work, in the sense that it makes me see nothing in the dos shell prompt.
Another thing I never understood is why the projects console shell prompt dos never close yourself but you have to do CTRL-C to terminate.
Can anyone give me the answers to that?

Ali Reza
19th June 2012, 21:03
a.exec() puts your program to a event loop, so if you dont want this you must remove return a.exec() and put return 0; instead.

giorgik
19th June 2012, 23:03
thanks Ali Reza ;) Now is the perfect