Pregunta

I am working with form application in VC++. I have main form i.e. Form1.h and also the child form named child.h. I am calling the child.h form on the button click of the form1.h. For calling the child.h I have to include Child.h in Form1.h.

I have used the following code in Form1.h

    #incude "Child.h"

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
     Child^ c=gcnew Child;
     c->Visible=true;
}

And In Child.h I am doing some processing. For this I have made one header file named param.h having some function name and global variables name. I have included param.h in Child.h file.

And param.h is

#ifndef param_h_seen
#define param_h_seen
#define LED_Line 4
#define CALIBRATION_MODE 0
typedef unsigned __int32 uint32_t;
typedef unsigned __int8 uint8_t;

/****for LED ROI entered by user***/
int x_of_roi=6;
int y_of_roi=10;
/********************************/

/*************for graph ROI*******/
int ROIwidth=16;
int ROIheight=4096;

/********************************/
int LED_num= 64;
unsigned short *calib_factor;
/*********functions*****************/

int find_area(unsigned char *intensity,int start);

void DetectRectangle();
/***************************************/


#endif

After Including the child.h It is showing the error

PUMA_LED_TESTER.obj : error LNK2005: "unsigned short * calib_factor" (?calib_factor@@3PAGA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int x_of_roi" (?x_of_roi@@3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int y_of_roi" (?y_of_roi@@3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int ROIwidth" (?ROIwidth@@3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int ROIheight" (?ROIheight@@3HA) already defined in Child.obj
PUMA_LED_TESTER.obj : error LNK2005: "int LED_num" (?LED_num@@3HA) already defined in Child.obj

I don't know why these errors are coming.Can any body please tell me the solution to solve these errors

Thanks in Advance

¿Fue útil?

Solución

int x_of_roi=6;
int y_of_roi=10;

Those are definitions, and should not be in your header files. Place them in one of the cpp files, and on the header have:

extern int x_of_roi
extern int y_of_roi;

Same goes with the rest of the global variables you declare in your header files. When those headers are included by more than one cpp file (namely translation unit), each unit effectively declares new variables with the same name, which the linker complains about.

Otros consejos

Each time you #include a header into you source file, the result is the same as just copy/pasting the header's text. So, if you have a header which defines something:

header.h:
int magic = 0xA0B1C2D3

And you include it into multiply cpp files:

source1.cpp:
#include "header.h"
<...>

source2.cpp:
#include "header.h"
<...>

The result is that the variables and macros are being defined for each cpp. In this case, it is ok. But if you have more complicated dependencies, this may result in errors that you are currently getting.

In your case you are basically including the same file twice, and that leads to multiply definitions of stuff with the same name. What you need to do is to place the definitions outside of header files, and use extern when you need to access them from elsewhere.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top