Results 1 to 6 of 6

Thread: Multiple definition of a variable?

  1. #1
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Multiple definition of a variable?

    Hi guys,

    got a little problem i created a new class and since this, i got a double defnition but i can't find it, is there somewhere a mistake in the class definition?

    Header:
    Qt Code:
    1. #ifndef SCHLICHTMUSTER_H
    2. #define SCHLICHTMUSTER_H
    3.  
    4. #include "step.h"
    5. #include "stack.h"
    6. #include "package.h"
    7.  
    8. #define VP 15
    9. #define P 0
    10. #define ZS 1
    11.  
    12. stack *sta;
    13. package *pac;
    14.  
    15. QVector<step> Schlichtmuster_A(int height, QString p1, QString s1);
    16. QVector<step> Schlichtmuster_A_ZS(int height, QString p1, QString s1);
    17.  
    18. #endif // SCHLICHTMUSTER_H
    To copy to clipboard, switch view to plain text mode 

    cpp:
    Qt Code:
    1. #include <QVector>
    2. #include "schlichtmuster.h"
    3.  
    4.  
    5. QVector<step> Schlichtmuster_A(int height, QString p1, QString s1){
    6. int anz_gesamt = 0;
    7. int anz_l,anz_b,anz_h;
    8. int dest_x, dest_y, dest_z, dest_dreh;
    9. int h = height;
    10. sta = new stack(s1);
    11. pac = new package(p1);
    12. QVector<step> steps;
    13.  
    14. anz_b = sta->width / pac->width;
    15. anz_l = sta->lenght / pac->lenght;
    16. anz_h = h / pac->height;
    17. anz_gesamt = 0;
    18.  
    19. dest_dreh =0;
    20. qDebug()<<anz_l<<" "<<anz_b<<" "<<anz_h;
    21.  
    22.  
    23. for(int i_h =0;i_h<=anz_h;i_h++){
    24. dest_z = sta->height+(i_h*pac->height);
    25.  
    26. for(int i_l = 0;i_l<anz_l;i_l++){
    27. dest_y = i_l*pac->lenght;
    28. for(int i_b = 0;i_b<anz_b;i_b++){
    29. dest_x = i_b*pac->width;
    30. qDebug()<<dest_x<<" "<<dest_y<<" "<<dest_z;
    31. anz_gesamt++;
    32. steps.insert(anz_gesamt,step(1,anz_gesamt,0,P,dest_x,dest_y,dest_z,dest_dreh,dest_x+VP,dest_y+VP,dest_z+VP,dest_dreh));
    33. }
    34. }
    35. }
    36.  
    37. return steps;
    38. }
    39.  
    40. QVector<step> Schlichtmuster_A_ZS(int height, QString p1, QString s1){
    41. int anz_gesamt = 0;
    42. int anz_l,anz_b,anz_h;
    43. int dest_x, dest_y, dest_z, dest_dreh;
    44. int h = height;
    45. sta = new stack(s1);
    46. pac = new package(p1);
    47. QVector<step> steps;
    48.  
    49. anz_b = sta->width / pac->width;
    50. anz_l = sta->lenght / pac->lenght;
    51. anz_h = h / pac->height;
    52. anz_gesamt = 0;
    53.  
    54. dest_dreh =0;
    55. qDebug()<<anz_l<<" "<<anz_b<<" "<<anz_h;
    56.  
    57.  
    58. for(int i_h =0;i_h<=anz_h;i_h++){
    59. dest_z = sta->height+(i_h*pac->height);
    60.  
    61. for(int i_l = 0;i_l<anz_l;i_l++){
    62. dest_y = i_l*pac->lenght;
    63. if(i_h>0){
    64. anz_gesamt++;
    65. steps.insert(anz_gesamt,step(1,anz_gesamt,0,ZS,0,0,dest_z,dest_dreh,0+VP,0+VP,dest_z+VP,dest_dreh));
    66. }
    67. for(int i_b = 0;i_b<anz_b;i_b++){
    68. dest_x = i_b*pac->width;
    69. qDebug()<<dest_x<<" "<<dest_y<<" "<<dest_z;
    70. anz_gesamt++;
    71. steps.insert(anz_gesamt,step(1,anz_gesamt,0,P,dest_x,dest_y,dest_z,dest_dreh,dest_x+VP,dest_y+VP,dest_z+VP,dest_dreh));
    72. }
    73. }
    74. }
    75.  
    76. return steps;
    77. }
    To copy to clipboard, switch view to plain text mode 

    hope some1 can help me

    thx tobi

  2. #2
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple definition of a variable?

    Sorry guy, but you have attached the wrong code.

    You have put no classes !! Only two functions that I cannot even compile !!

    You missed step.h, stack.h & package.h ( & *.cpp ) that perhaps contain the classes ?

  3. #3
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple definition of a variable?

    You can not define a variable in an header, or it will be defined every time you will include that header. Just declare the variable with the extern keyword and define in the cpp file:

    Header
    Qt Code:
    1. ...
    2. extern stack *sta;
    3. extern package *pac;
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Cpp
    Qt Code:
    1. #include <header.h>
    2. ...
    3. stack *sta;
    4. package *pac;
    5. ...
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple definition of a variable?

    @bender86:
    i used ifndef -> so the header is just included once isn't it?

    @jpujolf:
    sry forgot to attach them

    Qt Code:
    1. #ifndef STEP_H
    2. #define STEP_H
    3.  
    4. /*! \class Step
    5.  * \brief contains all informations for one step
    6.  *
    7.  *
    8. */
    9. class step {
    10.  
    11. public:
    12. step();
    13. step(int a, int b);
    14. step(int id_sm, int id_s, int id_src, int t, int x, int y, int z, int dr, int x_vp, int y_vp, int z_vp, int dr_vp);
    15.  
    16. int id_sm;
    17. int id_s;
    18. int id_src;
    19.  
    20. int type;
    21.  
    22. int dest_vp_x;
    23. int dest_vp_y;
    24. int dest_vp_z;
    25. int dest_vp_dreh;
    26. int dest_x;
    27. int dest_y;
    28. int dest_z;
    29. int dest_dreh;
    30.  
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "step.h"
    2.  
    3. #include <QtSql>
    4.  
    5. step::step(){
    6.  
    7. }
    8.  
    9. step::step(int id_sm1, int id_s1, int id_src1, int t1, int x1, int y1, int z1,
    10. int dr1, int x_vp1, int y_vp1, int z_vp1, int dr_vp1){
    11. id_sm = id_sm1;
    12. id_s = id_s1;
    13. id_src = id_src1;
    14. type = t1;
    15. dest_x = x1;
    16. dest_y = y1;
    17. dest_z = z1;
    18. dest_dreh = dr1;
    19. dest_vp_dreh = dr_vp1;
    20. dest_vp_x = x_vp1;
    21. dest_vp_y = y_vp1;
    22. dest_vp_z = z_vp1;
    23. }
    24.  
    25. step::step(int a,int b){
    26. QSqlQuery q ("select * from schlichtungsschritt where id_schlichtmuster='"
    27. +QString(a)+"' and id_schritt='"+QString(b)+"'");
    28. QSqlRecord r = q.record();
    29.  
    30. while(q.next()){
    31. id_s = q.value(r.indexOf("id_schritt")).toInt();
    32. id_sm = q.value(r.indexOf("id_schlichtmuster")).toInt();
    33. id_src = q.value(r.indexOf("id_src")).toInt();
    34. type = q.value(r.indexOf("type")).toInt();
    35. dest_x = q.value(r.indexOf("dest_x")).toInt();
    36. dest_y = q.value(r.indexOf("dest_y")).toInt();
    37. dest_z = q.value(r.indexOf("dest_z")).toInt();
    38. dest_dreh = q.value(r.indexOf("dest_drehung")).toInt();
    39. dest_vp_dreh = dest_x = q.value(r.indexOf("dest_vp_drehung")).toInt();
    40. dest_vp_x = q.value(r.indexOf("dest_vp_x")).toInt();
    41. dest_vp_y = q.value(r.indexOf("dest_vp_y")).toInt();
    42. dest_vp_z = q.value(r.indexOf("dest_vp_z")).toInt();
    43.  
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef STACK_H
    2. #define STACK_H
    3.  
    4. #include <QObject>
    5.  
    6.  
    7. /*! \class Stack
    8.  * \brief consists of three Integers (lenght,width and height) and a QString(name)
    9.  *
    10.  *
    11. */
    12. class stack {
    13.  
    14. public:
    15. stack();
    16. stack(QString s);
    17.  
    18.  
    19. QString bez;
    20. int lenght;
    21. int width;
    22. int height;
    23.  
    24. };
    25.  
    26.  
    27. #endif // STACK_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QTSql>
    2. #include "stack.h"
    3.  
    4. /*! \brief Dummy constructor */
    5. stack::stack(){
    6. bez = "Palette EUR";
    7. height = 144;
    8. width = 800;
    9. lenght = 1200;
    10. }
    11.  
    12. /*! \brief initializes stack from DB */
    13. stack::stack(QString s){
    14.  
    15. QSqlQuery q("select * from ladeeinheit where bezeichnung='"+
    16. s.trimmed()+"'");
    17. QSqlRecord r = q.record();
    18.  
    19. height =(q.value(r.indexOf("ladeeh_h"))).toInt();
    20. width = (q.value(r.indexOf("ladeeh_b"))).toInt();
    21. lenght = (q.value(r.indexOf("ladeeh_w"))).toInt();
    22. bez = ((q.value(r.indexOf("bezeichnung"))).toString());
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef PACKAGE_H
    2. #define PACKAGE_H 1
    3. #include <QtCore>
    4.  
    5.  
    6. /*! \class Package
    7.  * \brief consists of four Integers (lenght,width, height and type) and a QString(name)
    8.  *
    9.  *
    10. */
    11. class package {
    12.  
    13. public:
    14. package();
    15. package(QString s);
    16.  
    17. QString bez;
    18. int lenght;
    19. int width;
    20. int height;
    21. int type;
    22.  
    23. };
    24. #endif // PACKAGE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QTSql>
    2. #include "package.h"
    3.  
    4. /*! \brief Dummy constructor */
    5. package::package(){
    6. bez = "Oreo - Kekse";
    7. type = 0;
    8. height = 40;
    9. width = 200;
    10. lenght = 400;
    11. }
    12.  
    13. /*! \brief initializes package from DB */
    14. package::package(QString s){
    15.  
    16. QSqlQuery q("select * from paket where bezeichnung='"+
    17. s.trimmed()+"'");
    18. QSqlRecord r = q.record();
    19.  
    20. height =(q.value(r.indexOf("paket_h"))).toInt();
    21. width = (q.value(r.indexOf("paket_b"))).toInt();
    22. lenght = (q.value(r.indexOf("paket_w"))).toInt();
    23. type = (q.value(r.indexOf("type"))).toInt();
    24. bez = ((q.value(r.indexOf("bezeichnung"))).toString());
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    cause i didn't know how to attach tha actual files is copied them in

  5. #5
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple definition of a variable?

    ok i solved, i actually don't know what my mistake was, but i got it to run again through putting

    Qt Code:
    1. stack *sta;
    2. package *pac;
    To copy to clipboard, switch view to plain text mode 

    from the header directly to the functions, it's not great but it works :/

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple definition of a variable?

    i used ifndef -> so the header is just included once isn't it?
    it prevents including it twice in this situation:
    header1.h:
    Qt Code:
    1. // some code
    To copy to clipboard, switch view to plain text mode 
    header2.h:
    Qt Code:
    1. #include "header1.h"
    To copy to clipboard, switch view to plain text mode 
    code.cpp:
    Qt Code:
    1. #include "header1.h"
    2. #include "header2.h"
    3. // some code
    To copy to clipboard, switch view to plain text mode 
    as you can see, without those ifndefs file header1.h would be included twice to the code.cpp - first time directly, and second trough header2.h. And that is what for ifndefs are.

    But if you do like in your header file:
    header.h:
    Qt Code:
    1. int number;
    To copy to clipboard, switch view to plain text mode 
    code1.cpp:
    Qt Code:
    1. #include "header.h"
    2. // code...
    To copy to clipboard, switch view to plain text mode 
    code2.cpp
    Qt Code:
    1. #include "header.h"
    2. // code...
    To copy to clipboard, switch view to plain text mode 
    as you see header.h will be include to code1.cpp and will be also included to code2.cpp which is correct. But if you have definition of global variable in this header, then this definition would be seen while compiling code1.cpp and then, again, when compiling code2.cpp and that is the error you have.
    The proper way is to define those variables in one .cpp file and in other files when you need them, you have to use extern keyword, like:
    Qt Code:
    1. extern int number;
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. "Multiple definition of 'gridLayout'"
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 8th January 2008, 20:13
  2. Multiple Definition errors in moc_<Obj>.cpp and moc_<Obj>_obj.cpp
    By Doug Broadwell in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2007, 19:10
  3. Getting multiple definition error from qatomic_x86_64.h
    By markcole in forum General Programming
    Replies: 1
    Last Post: 30th August 2007, 19:39
  4. FreeBSD definition
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2006, 08:59
  5. Q3CanvasView Definition
    By Kapil in forum Newbie
    Replies: 4
    Last Post: 9th May 2006, 12:00

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.