Results 1 to 11 of 11

Thread: "Multiple definition of 'gridLayout'"

  1. #1
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default "Multiple definition of 'gridLayout'"

    I'm just starting out in C++ (coded a lot in Java and some hobby stuff in PHP) and I just ran into this weird problem:

    I have 2 classes, MainWindow (QMainWindow) & PokerTableInfo (QWidget). The layout for both of them uses a gridLayout. The MainWindow consists of a label and multiple instances of PokerTableInfo (so I got an #include PokerTableInfo.h in MainWindow). Now, so far so good, everything actually worked fine, but then I started cleaning up some code (MainWindow originally had multiple inheritance and it was unnecessary & made things ugly). In the declaration of both classes I have:

    Qt Code:
    1. private:
    2. QGridLayout *gridLayout;
    To copy to clipboard, switch view to plain text mode 

    Now, for some reason the linker is telling me that there's multiple definitions of gridLayout. I don't quite get this... Sure, the variables have the same type & name, but they're both in a completely different class (and private on top of that as well, not that it should matter). I have a MainWindow.gridLayout and a MainWindow.PokerTableInfoInstance.gridLayout, how and where does this become a problem?

    I'm used to working in Java and I never encountered something weird like that even though I use the same variable name for general stuff all the time (String lblName, etc.,...). So, can anyone explain to me why I'm getting this error and how to fix it? (Apart from the obvious renaming of the variable.)

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: "Multiple definition of 'gridLayout'"

    telling me that there's multiple definitions of gridLayout.
    It should also tell you where the previous deceleration is.

    Are you headers protected with #ifdef??
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Yes, everything's protected with #ifndef. And indeed, it does say where it's defined previously, it's just that I can't make sense of it. The errors point to 2 private pointers with the same name inside 2 different classes. Like this:

    Qt Code:
    1. Class Sheep : public QMainWindow
    2. {
    3. ...
    4.  
    5. private:
    6. QGridLayout *gridLayout;
    7. ...
    8. };
    9.  
    10. Class Cow : public QWidget
    11. {
    12. ...
    13.  
    14. private:
    15. QGridLayout *gridLayout;
    16. ...
    17. };
    To copy to clipboard, switch view to plain text mode 

    I really don't get what I'm doing wrong. There's a Q_OBJECT macro in both the class definitions, maybe it's that? The explanation in the help files on what it does exactly is really vague, so I'm not sure. I does say that it has to be there though, so I can't really delete it.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Its really hard to tell like that.
    Such errors are coding errors, and with out the relevant code, its almost impossible to tell where the problem is.
    If its a small project, zip and post it, so that we can try and compile it, or post all the relevant code, if it not too much.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Ok, well, the code that's giving errors isn't too big, so I'll post it here. Note that these are just the header files, the .cpp's are included in the attached zip file.

    PokerInfoTable.h
    Qt Code:
    1. #include <Qt stuff here (multiple lines)>
    2.  
    3. #include <string>
    4. #include <vector>
    5.  
    6. class PokerTableInfo : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. PokerTableInfo(QWidget *parent = 0);
    12. ~PokerTableInfo();
    13.  
    14. void setTableName(std::string *tableName);
    15. std::string *getTableName();
    16.  
    17. void setFolded();
    18. void newRound();
    19.  
    20. void setNumPlayers(int numPlayers);
    21. void setProfit(double *dProfit);
    22.  
    23. void setHandFlavorText(std::string *sFlavor);
    24.  
    25. void addHandCard(char rank, char suit);
    26. void addBoardCard(char rank, char suit);
    27.  
    28. private:
    29. QGroupBox *boxTable;
    30. QWidget *layoutWidget;
    31. QGridLayout *gridLayout;
    32. QLabel *lblPlayers;
    33. QLabel *txtPlayers;
    34. QLabel *lblHand;
    35. QLabel *txtHand;
    36. QLabel *lblBoard;
    37. QLabel *txtBoard;
    38. QLabel *lblProfit;
    39. QLabel *txtProfit;
    40.  
    41. bool folded;
    42.  
    43. std::vector<char> handCardsRank;
    44. std::vector<char> handCardsSuit;
    45. QString *handFlavorText;
    46.  
    47. std::vector<char> boardCardsRank;
    48. std::vector<char> boardCardsSuit;
    49.  
    50. std::string *tableName;
    51.  
    52. void setupUi();
    53.  
    54. void resetHandCards();
    55. void resetBoardCards();
    56.  
    57. void setHandText();
    58. void setBoardText();
    59.  
    60. static bool isLegalRank(char rank);
    61. static bool isLegalSuit(char suit);
    62.  
    63. static void getCardText(std::vector<char> *rank, std::vector<char> *suit, QString *cardText);
    64. };
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2.  
    3. #include <vector>
    4.  
    5. #include "PokerTableInfo.h"
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. std::vector<PokerTableInfo*> getPokerTables();
    16. void addPokerTable();
    17. void removePokerTable(int tableNr);
    18.  
    19. private:
    20. std::vector<PokerTableInfo*> pokerTables;
    21.  
    22. void autoResize(int numPokerTables);
    23. void autoRearange();
    24.  
    25. void setNewSize(int width, int height);
    26.  
    27. void setupUi();
    28.  
    29. QWidget *centralwidget;
    30. QWidget *gridLayoutContainer;
    31. QGridLayout *gridLayout;
    32. PokerTableInfo *pokerTableInfo;
    33. QLabel *lblStatusBar;
    34. QLabel *lblEmpty;
    35. };
    To copy to clipboard, switch view to plain text mode 

    The error says gridLayout is already defined in MainWindow.h (4th line from the bottom) and it's being redefined in PokerInfoTable.h (3rd line after private declarations start).

    In case you want to see for yourself, I made a minimalist project that still gives exactly the same error, just run compile.bat in the attached zip file.
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "Multiple definition of 'gridLayout'"

    What are these variables for in PokerTableInfo.cpp?
    Qt Code:
    1. ...
    2. QGroupBox *boxTable;
    3. QWidget *layoutWidget;
    4. QGridLayout *gridLayout;
    5. QLabel *lblPlayers;
    6. QLabel *txtPlayers;
    7. QLabel *lblHand;
    8. QLabel *txtHand;
    9. QLabel *lblBoard;
    10. QLabel *txtBoard;
    11. QLabel *lblProfit;
    12. QLabel *txtProfit;
    13. ...
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Multiple definition of 'gridLayout'"

    They are the elements composing the PokerTableInfo widget. I originally made it in Designer, compiled the .ui file and then just took the parts I needed for Ui_PokerInfoTable.h and put them in the PokerTableInfo class (so that I didn't have to let it inherit Ui_PokerInfoTable). It works fine if I change the name of the gridLayout variable to gridLayout1.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Quote Originally Posted by Darhuuk View Post
    I originally made it in Designer, compiled the .ui file and then just took the parts I needed for Ui_PokerInfoTable.h and put them in the PokerTableInfo class
    These variables are global, they don't belong to any class.

    Quote Originally Posted by Darhuuk View Post
    so that I didn't have to let it inherit Ui_PokerInfoTable
    You don't have to inherit from Ui:xxx classes to use them. You can use aggregation instead. With your approach you have lost possibility to change the layout with Designer.

    Quote Originally Posted by Darhuuk View Post
    It works fine if I change the name of the gridLayout variable to gridLayout1.
    That's because you never use those variables.

  9. The following user says thank you to jacek for this useful post:

    Darhuuk (8th January 2008)

  10. #9
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Quote Originally Posted by jacek View Post
    These variables are global, they don't belong to any class.
    Ah, I finally get it now. I thought I had to redeclare the variables declared in the header file in the .cpp file. Beginner's mistake I guess . Thanks for helping me realize.

    Quote Originally Posted by jacek View Post
    You don't have to inherit from Ui:xxx classes to use them. You can use aggregation instead. With your approach you have lost possibility to change the layout with Designer.
    Hm, yes, true, I'm aware of that. It's just that I found it cleaner this way, otherwise every time I wanted to change something in the widget during runtime I would have had to reference to the aggregated class (Ok, only a little bit more typing, but meh). And it's a very small design, I could have made it by hand. If it were big with tons of widgets in it, I would have kept it as it originally was so that I could still edit it in Designer.

    Thanks again, stupid C++ newbie problem #10000 solved !

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Quote Originally Posted by Darhuuk View Post
    I thought I had to redeclare the variables declared in the header file in the .cpp file.
    You have to do this only for static variables.

  12. The following user says thank you to jacek for this useful post:

    Darhuuk (8th January 2008)

  13. #11
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Multiple definition of 'gridLayout'"

    Quote Originally Posted by jacek View Post
    You have to do this only for static variables.
    Oh, ok, thanks!

Similar Threads

  1. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

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.