PDA

View Full Version : Multiple definition of a variable?



T0bi4s
14th January 2010, 16:47
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:


#ifndef SCHLICHTMUSTER_H
#define SCHLICHTMUSTER_H

#include "step.h"
#include "stack.h"
#include "package.h"

#define VP 15
#define P 0
#define ZS 1

stack *sta;
package *pac;

QVector<step> Schlichtmuster_A(int height, QString p1, QString s1);
QVector<step> Schlichtmuster_A_ZS(int height, QString p1, QString s1);

#endif // SCHLICHTMUSTER_H



cpp:


#include <QVector>
#include "schlichtmuster.h"


QVector<step> Schlichtmuster_A(int height, QString p1, QString s1){
int anz_gesamt = 0;
int anz_l,anz_b,anz_h;
int dest_x, dest_y, dest_z, dest_dreh;
int h = height;
sta = new stack(s1);
pac = new package(p1);
QVector<step> steps;

anz_b = sta->width / pac->width;
anz_l = sta->lenght / pac->lenght;
anz_h = h / pac->height;
anz_gesamt = 0;

dest_dreh =0;
qDebug()<<anz_l<<" "<<anz_b<<" "<<anz_h;


for(int i_h =0;i_h<=anz_h;i_h++){
dest_z = sta->height+(i_h*pac->height);

for(int i_l = 0;i_l<anz_l;i_l++){
dest_y = i_l*pac->lenght;
for(int i_b = 0;i_b<anz_b;i_b++){
dest_x = i_b*pac->width;
qDebug()<<dest_x<<" "<<dest_y<<" "<<dest_z;
anz_gesamt++;
steps.insert(anz_gesamt,step(1,anz_gesamt,0,P,dest _x,dest_y,dest_z,dest_dreh,dest_x+VP,dest_y+VP,des t_z+VP,dest_dreh));
}
}
}

return steps;
}

QVector<step> Schlichtmuster_A_ZS(int height, QString p1, QString s1){
int anz_gesamt = 0;
int anz_l,anz_b,anz_h;
int dest_x, dest_y, dest_z, dest_dreh;
int h = height;
sta = new stack(s1);
pac = new package(p1);
QVector<step> steps;

anz_b = sta->width / pac->width;
anz_l = sta->lenght / pac->lenght;
anz_h = h / pac->height;
anz_gesamt = 0;

dest_dreh =0;
qDebug()<<anz_l<<" "<<anz_b<<" "<<anz_h;


for(int i_h =0;i_h<=anz_h;i_h++){
dest_z = sta->height+(i_h*pac->height);

for(int i_l = 0;i_l<anz_l;i_l++){
dest_y = i_l*pac->lenght;
if(i_h>0){
anz_gesamt++;
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));
}
for(int i_b = 0;i_b<anz_b;i_b++){
dest_x = i_b*pac->width;
qDebug()<<dest_x<<" "<<dest_y<<" "<<dest_z;
anz_gesamt++;
steps.insert(anz_gesamt,step(1,anz_gesamt,0,P,dest _x,dest_y,dest_z,dest_dreh,dest_x+VP,dest_y+VP,des t_z+VP,dest_dreh));
}
}
}

return steps;
}


hope some1 can help me :confused::rolleyes:

thx tobi

jpujolf
14th January 2010, 17:01
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 ?

bender86
14th January 2010, 17:38
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
...
extern stack *sta;
extern package *pac;
...
Cpp
#include <header.h>
...
stack *sta;
package *pac;
...

T0bi4s
14th January 2010, 19:28
@bender86:
i used ifndef -> so the header is just included once isn't it?

@jpujolf:
sry forgot to attach them :rolleyes:


#ifndef STEP_H
#define STEP_H

/*! \class Step
* \brief contains all informations for one step
*
*
*/
class step {

public:
step();
step(int a, int b);
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);

int id_sm;
int id_s;
int id_src;

int type;

int dest_vp_x;
int dest_vp_y;
int dest_vp_z;
int dest_vp_dreh;
int dest_x;
int dest_y;
int dest_z;
int dest_dreh;


};

#include "step.h"

#include <QtSql>

step::step(){

}

step::step(int id_sm1, int id_s1, int id_src1, int t1, int x1, int y1, int z1,
int dr1, int x_vp1, int y_vp1, int z_vp1, int dr_vp1){
id_sm = id_sm1;
id_s = id_s1;
id_src = id_src1;
type = t1;
dest_x = x1;
dest_y = y1;
dest_z = z1;
dest_dreh = dr1;
dest_vp_dreh = dr_vp1;
dest_vp_x = x_vp1;
dest_vp_y = y_vp1;
dest_vp_z = z_vp1;
}

step::step(int a,int b){
QSqlQuery q ("select * from schlichtungsschritt where id_schlichtmuster='"
+QString(a)+"' and id_schritt='"+QString(b)+"'");
QSqlRecord r = q.record();

while(q.next()){
id_s = q.value(r.indexOf("id_schritt")).toInt();
id_sm = q.value(r.indexOf("id_schlichtmuster")).toInt();
id_src = q.value(r.indexOf("id_src")).toInt();
type = q.value(r.indexOf("type")).toInt();
dest_x = q.value(r.indexOf("dest_x")).toInt();
dest_y = q.value(r.indexOf("dest_y")).toInt();
dest_z = q.value(r.indexOf("dest_z")).toInt();
dest_dreh = q.value(r.indexOf("dest_drehung")).toInt();
dest_vp_dreh = dest_x = q.value(r.indexOf("dest_vp_drehung")).toInt();
dest_vp_x = q.value(r.indexOf("dest_vp_x")).toInt();
dest_vp_y = q.value(r.indexOf("dest_vp_y")).toInt();
dest_vp_z = q.value(r.indexOf("dest_vp_z")).toInt();

}
}



#ifndef STACK_H
#define STACK_H

#include <QObject>


/*! \class Stack
* \brief consists of three Integers (lenght,width and height) and a QString(name)
*
*
*/
class stack {

public:
stack();
stack(QString s);


QString bez;
int lenght;
int width;
int height;

};


#endif // STACK_H


#include <QTSql>
#include "stack.h"

/*! \brief Dummy constructor */
stack::stack(){
bez = "Palette EUR";
height = 144;
width = 800;
lenght = 1200;
}

/*! \brief initializes stack from DB */
stack::stack(QString s){

QSqlQuery q("select * from ladeeinheit where bezeichnung='"+
s.trimmed()+"'");
QSqlRecord r = q.record();

height =(q.value(r.indexOf("ladeeh_h"))).toInt();
width = (q.value(r.indexOf("ladeeh_b"))).toInt();
lenght = (q.value(r.indexOf("ladeeh_w"))).toInt();
bez = ((q.value(r.indexOf("bezeichnung"))).toString());

}



#ifndef PACKAGE_H
#define PACKAGE_H 1
#include <QtCore>


/*! \class Package
* \brief consists of four Integers (lenght,width, height and type) and a QString(name)
*
*
*/
class package {

public:
package();
package(QString s);

QString bez;
int lenght;
int width;
int height;
int type;

};
#endif // PACKAGE_H


#include <QTSql>
#include "package.h"

/*! \brief Dummy constructor */
package::package(){
bez = "Oreo - Kekse";
type = 0;
height = 40;
width = 200;
lenght = 400;
}

/*! \brief initializes package from DB */
package::package(QString s){

QSqlQuery q("select * from paket where bezeichnung='"+
s.trimmed()+"'");
QSqlRecord r = q.record();

height =(q.value(r.indexOf("paket_h"))).toInt();
width = (q.value(r.indexOf("paket_b"))).toInt();
lenght = (q.value(r.indexOf("paket_w"))).toInt();
type = (q.value(r.indexOf("type"))).toInt();
bez = ((q.value(r.indexOf("bezeichnung"))).toString());

}



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

T0bi4s
14th January 2010, 21:12
ok i solved, i actually don't know what my mistake was, but i got it to run again through putting



stack *sta;
package *pac;


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

faldzip
14th January 2010, 22:13
i used ifndef -> so the header is just included once isn't it?

it prevents including it twice in this situation:
header1.h:


// some code

header2.h:


#include "header1.h"

code.cpp:


#include "header1.h"
#include "header2.h"
// some code

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:


int number;

code1.cpp:


#include "header.h"
// code...

code2.cpp


#include "header.h"
// code...

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:


extern int number;