Results 1 to 4 of 4

Thread: #define id (expression) Directive generates error

  1. #1
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default #define id (expression) Directive generates error

    Hi,

    i come to some unpredictable state,

    first declared in file1.h
    /* File1.h */

    typedef struct {
    int i;
    } struct_1;
    #define size_1 10
    #define DATA_LEN (size_1 + sizeof(struct_1) )

    importing file1.h in to file2.h

    #include "File1.h"

    char text_data[ DATA_LEN ]; // here i am getting error expected ']' before ';' token

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: #define id (expression) Directive generates error

    Two problems with your code:
    1) use include guards in headers
    2) do not define variables in header files, this way each *.c file which includes your header will get its own copy of the variable

    to solve 2), use the "extern" specifier

    apart from that, your example seems to compile just fine on gcc 4.5:
    Qt Code:
    1. // this code is wrong, correct it by applying 1) and 2)
    2. //
    3. //
    4. // file1.h
    5. typedef struct {
    6. int i;
    7. } struct_1;
    8. #define size_1 10
    9. #define DATA_LEN (size_1 + sizeof(struct_1) )
    10.  
    11. //file2.h
    12. #include "file1.h"
    13.  
    14. char text_data[ DATA_LEN ];
    15.  
    16. //main.c
    17. #include "file2.h"
    18.  
    19. int main(){
    20. return 0;
    21. }
    22.  
    23. // compiled ok with :
    24. // gcc main.c -Werror -Wall -pedantic
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: #define id (expression) Directive generates error

    Quote Originally Posted by urvil View Post
    Hi,

    i come to some unpredictable state,

    first declared in file1.h
    /* File1.h */

    typedef struct {
    int i;
    } struct_1;
    #define size_1 10
    #define DATA_LEN (size_1 + sizeof(struct_1) )

    importing file1.h in to file2.h

    #include "File1.h"

    char text_data[ DATA_LEN ]; // here i am getting error expected ']' before ';' token
    Is that the exact code you have? Are you sure there is no semicolon in any of the defines?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: #define id (expression) Directive generates error

    Thanks wysota,
    Your thinking is right, there were a semicolon in define line but there is lots of spaces in between semicolon n sentance
    #define DATA_LEN (size_1 + sizeof(struct_1) ) (long string of spaces) ........................................... ;
    I am wondering how this happen...

    Thanks for the reply
    Last edited by urvil; 31st May 2015 at 11:50.

Similar Threads

  1. Regular Expression to match preprocessor directive statement
    By parulkalra in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2013, 23:33
  2. Replies: 11
    Last Post: 29th October 2013, 09:13
  3. Negative border offset CSS in windows generates error
    By droneone in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2013, 20:42
  4. Replies: 5
    Last Post: 21st May 2011, 02:06
  5. error: invalid use of void expression
    By ChineseGeek in forum Qt Programming
    Replies: 17
    Last Post: 3rd October 2009, 09:59

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.