Results 1 to 7 of 7

Thread: multiple definitoin of variable

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default multiple definitoin of variable

    I need to define my queue globally in a header file. And I of course include that header in other classes.
    but I constantly receive multiple definition of variable xx.

    how can I declare it only once? so for the next times compiler ignores it?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: multiple definitoin of variable

    You need to define the variable in a cpp file (not in header file).
    The header file will have just declaration of the variable with "extern" keyword as prefix, and without intilaization.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: multiple definitoin of variable

    For extern variable:
    ex: extern QList mylist;

    For
    but I constantly receive multiple definition of variable xx.
    Use header guards,
    Ex:
    #ifndef GLOBAL_H
    ........
    #endif

    hope it helps,
    Bala

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: multiple definitoin of variable

    Quote Originally Posted by BalaQT
    For
    Quote Originally Posted by saman_artorious
    but I constantly receive multiple definition of variable xx.
    Use header guards,
    Ex:
    #ifndef GLOBAL_H
    ........
    #endif

    hope it helps,
    Bala
    Using guard macro in header file is a good practice, but it will not solve the multiple definition problem/error
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple definitoin of variable

    Quote Originally Posted by saman_artorious View Post
    I need to define my queue globally in a header file.
    no, you don't.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: multiple definitoin of variable

    Quote Originally Posted by amleto View Post
    no, you don't.
    if queue is defined privately in my class, other object cannot get access to it and enQ. so, I have to set it as global. I cannot pass the QByteArray to the producer of the thread, so that it enQ it to the queue.

  7. #7
    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: multiple definitoin of variable

    Quote Originally Posted by saman_artorious View Post
    if queue is defined privately in my class, other object cannot get access to it and enQ. so, I have to set it as global.
    No, you don't have to set it global. But even if you did, you wouldn't do it in a header file as these get included into multiple compilation units causing mutliple declaration problems as the one you're suffering from right now. I think this is a good moment to suggest spending some time with a good book on object-oriented programming before tackling real problems.
    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.


Similar Threads

  1. How to set common variable for multiple Dailogs in Qt
    By jayapandiyan in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2013, 15:08
  2. State variable used by multiple threads
    By bryang in forum Qt Programming
    Replies: 4
    Last Post: 29th April 2012, 20:08
  3. multiple table in a single variable
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2011, 11:01
  4. Replies: 2
    Last Post: 9th December 2010, 16:49
  5. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 22:13

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.