Results 1 to 5 of 5

Thread: Example use namespace

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Example use namespace

    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
    Qt Code:
    1. #ifndef GEOMETRIA_H
    2. #define GEOMETRIA_H
    3.  
    4. namespace Geometria {
    5. float Norma(int i) {
    6. float risultato = (float)i/6;
    7. return risultato;
    8. }
    9. }
    10.  
    11. #endif // GEOMETRIA_H
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include "geometria.h"
    4.  
    5. using namespace std;
    6. using namespace Geometria;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. float ris = Norma(3);
    13. cout << "Il risultato è: " << ris << endl;
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example use namespace

    In this simple example You don't need a QCoreApplication instance. Just remove lines 10 and 15.

  3. #3
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example use namespace

    Hi Lesiok, then I have to write code that is
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include "geometria.h"
    4.  
    5. using namespace std;
    6. using namespace Geometria;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. float ris = Norma(3);
    11. cout << "Il risultato è: " << ris << endl;
    12.  
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    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):
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include "geometria.h"
    4.  
    5. using namespace std;
    6. using namespace Geometria;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. float ris = Norma(3);
    13. cout << "Il valore e': " << ris << endl;
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by giorgik; 19th June 2012 at 18:56.

  4. #4
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: Example use namespace

    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.
    Life is about making the right decisions and moving on.

  5. #5
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example use namespace

    thanks Ali Reza Now is the perfect

Similar Threads

  1. Ui namespace
    By ashboi in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2012, 00:00
  2. using namespace VS using
    By jryans10 in forum General Programming
    Replies: 1
    Last Post: 7th January 2012, 22:23
  3. QTM namespace
    By Aman607 in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2011, 08:00
  4. using namespace with VS integration
    By jan in forum Qt Tools
    Replies: 0
    Last Post: 29th September 2009, 04:20
  5. namespace problem
    By mhoover in forum Qt Programming
    Replies: 4
    Last Post: 11th July 2006, 22:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.