Results 1 to 7 of 7

Thread: #ifndef syntax error

  1. #1
    Join Date
    Feb 2006
    Location
    Knoxville, TN
    Posts
    14
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default #ifndef syntax error

    I'm still working on subclassing an external library, and have decided to use preprocessor directives to set my .h file up so the library will work with or without Qt.

    For compiling my library without Qt I define a compiler flag -DNOQT.

    In my .h file I put

    Qt Code:
    1. #ifndef NOQT
    2. class Server: public QObject{
    3. Q_OBJECT
    4. #else
    5. class Server{
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    I can compile this just fine without Qt. But with Qt I get the following errror:
    Error: syntax error

    When I try the following it works. But of course then I can't compile the library without Qt:
    Qt Code:
    1. #ifndef NOQT
    2. class Server: public QObject{
    3. Q_OBJECT
    4. //#else
    5. //class Server{
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    Any ideas on fixing this? It would be really great if I could maintain just one .h file.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    I can't see anything wrong but I have a workaround:
    Qt Code:
    1. #ifndef NOQT
    2. class Server: public QObject{
    3. #else
    4. #undef Q_OBJECT
    5. class Server{
    6. #endif
    7. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 
    If the error persists please post more code.

  3. #3
    Join Date
    Feb 2006
    Location
    Knoxville, TN
    Posts
    14
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    When I do that I get
    ../my_sockets/my_sockets.h:46: Warning: Cannot use Q_OBJECT in nested class.
    ../my_sockets/my_sockets.h:78: Error: syntax error
    make: *** [.moc/moc_my_sockets.cpp] Error 1
    Below is my complete header file, just in case you see something else that may cause this. I had a few more #ifndef statements, but commented them out to make sure that the one I'm looking at is really the offending one.

    Qt Code:
    1. #ifndef MYSOCKETS_H
    2. #define MYSOCKETS_H
    3.  
    4. #include "rbTree.h"
    5. #include "Fields.h"
    6.  
    7. // just for file testing
    8. #include <iostream>
    9. #include <fstream>
    10. //#include <csignal>
    11.  
    12. //#ifndef NOQT
    13. #include <qframe.h>
    14. #include <qobject.h>
    15. //#endif
    16.  
    17.  
    18. using namespace std;
    19.  
    20. // global methods
    21. void* server_thread_stub(void*);
    22. void* server_rcv_thread_stub(void*);
    23. void* client_rcv_thread_stub(void*);
    24.  
    25.  
    26. typedef struct position{
    27. int lateral;
    28. int distance;
    29. int vertical;
    30. };
    31.  
    32. typedef struct client_info{
    33. int fd;
    34. bool master;
    35. char ip[20];
    36. char name[100];
    37. int video;
    38. };
    39.  
    40. #ifndef NOQT
    41. class Server: public QObject{
    42. Q_OBJECT
    43. #else
    44. class Server{
    45. #endif
    46.  
    47. public:
    48. Server(int);
    49. virtual ~Server();
    50. int server_thread(void);
    51. int getImage();
    52. // client functions
    53. int connectClient(int);
    54. int send();
    55. int sendImage(char *);
    56. friend int receive_message(int);
    57. friend void* server_rcv_thread_stub(void*);
    58. friend void* client_rcv_thread_stub(void*);
    59. rbTree<int> client_tree;
    60.  
    61. //Server( QObject *parent=0, const char *name=0 );
    62. //#ifndef NOQT
    63. signals:
    64. void updateClientList();
    65. //#endif
    66.  
    67. private:
    68. static unsigned long int tid;
    69.  
    70. int port;
    71. int newsockfd;
    72. int newClientsockfd;
    73.  
    74. };
    75.  
    76. #endif //MYSOCKETS_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Österreich
    Posts
    35
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    Probably moc is not liking this. Why not just make two complete but separate versions of the class and put your #ifndef-else statement entirely around the one using QT instead of just the first two lines? I think that way moc would be happier and it's not like at compile time the second class is created anyway.
    Last edited by michel; 13th February 2006 at 21:57.
    My philosophy is: If you can use a free, open-source alternative: do it. And if you can't, pretend it's free and open-source and hope you don't get caught.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    As usual, macros are evil...

    Maybe we can find a better way to do this: Everything in your library should work without Qt. Then create thin wrapper classes for Qt which derive from QObject and the particular class. That way you can avoid those macros.

  6. #6
    Join Date
    Feb 2006
    Location
    Knoxville, TN
    Posts
    14
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    I was able to get it to work by putting two separate class declarations inside the #ifndef #else statements. This solution is acceptable, especially since I don't have much time.

    Although I gave Codepoet's idea a quick try, since this would certainly be more elegant. But the problem is that I do have an #ifndef statement inside my cpp code so that I can emit a Qt signal. If I set my classes up so that the "Qt-enabled" server class inherits from the "Qt-ignorant" class, it complains about the emit signal in the cpp code not being defined in the base class. And there is no easy way around this. So I'll go with the first approach.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: #ifndef syntax error

    Quote Originally Posted by cbeall1
    Although I gave Codepoet's idea a quick try, since this would certainly be more elegant. But the problem is that I do have an #ifndef statement inside my cpp code so that I can emit a Qt signal. If I set my classes up so that the "Qt-enabled" server class inherits from the "Qt-ignorant" class, it complains about the emit signal in the cpp code not being defined in the base class. And there is no easy way around this. So I'll go with the first approach.
    For the next time: You can instead call a protected virtual member function which does nothing in the base class and in the Qt class you can override this method to call emit

Similar Threads

  1. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 11:15
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 14:43
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  4. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 03:49
  5. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 13:19

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.