| |

VerySource

 Forgot password?
 Register
Search
View: 833|Reply: 6

Urgent question: the structure passed as a global variable in different classes, thank you! !!

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-4 20:30:01
| Show all posts |Read mode
I am using VC to make a single document project proj.
Contains files such as mainfrm.cpp; proj.cpp; projDoc.cpp; projView.cpp.
A header file defining global variables is included in the project: "globlevariable.h"
The structure is defined in the above header file and the corresponding global variables are declared:
///
.....

struct g_sCtrldata
{
struct g_sCtrldata * pNext;
struct g_sCtrldata * head;
double x;
double y;
double dy;
double ddy;
}; // Structure definition
g_sCtrldata * g_ContrldataHipA; // Global variable declaration
.....
//
Then I include this header file in Mainfrm.cpp and assign a value to the global variable * g_ContrldataHipA.
Mainfrm.cpp:
///
#include "globlevariable.h"
g_ContrldataHipA = (g_sCtrldata *) malloc (sizeof (g_sCtrldata) * 100);
.....
for (int i = 0; i <100; i ++)
{(g_ContrldataHipA + i)-> pNext = pI + 1;
(g_ContrldataHipA + i)-> x = * (g_dOutX + i);
(g_ContrldataHipA + i)-> y = * (g_dOutY + i);
(g_ContrldataHipA + i)-> dy = * (g_dOutDY + i);
(g_ContrldataHipA + i)-> ddy = * (g_dOutDDY + i);
(g_ContrldataHipA + i)-> head = g_ContrldataHipA;
Ranch
}
...
All the above went well.
But when I want to display the values ​​in the g_ContrldataHipA array with a curve, I need to access and modify it in projView.cpp. Then the problem came out:
When using g_ContrldataHipA, an error is displayed during compilation, and g_ContrldataHipA is not defined.
I also use extern g_ContrldataHipA.
But other non-structural global variables in globalvariable.h can be used directly, such as double precision variables, integer variables, and so on.

Later I thought of adding #include "globlevariable.h" to projView.cpp
But this time prompted hundreds of errors, because "globlevariable.h" is already defined in main.obj. : Error LNK2015: "struct g_sCtrldata * g_Contrldata" (? G_Contrldata @@ 3PAUg_sCtrldata @@ A) already defined in MainFrm.obj

Please masters to help me solve this problem, is it not possible to pass the structure as a global variable in different classes?
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-19 00:15:02
| Show all posts
globlevariable.h changed to:
struct g_sCtrldata
{
struct g_sCtrldata * pNext;
struct g_sCtrldata * head;
double x;
double y;
double dy;
double ddy;
}; // Structure definition
extern bg_sCtrldata * g_ContrldataHipA; // Global variable declaration

Then add #include "globlevariable.h" in projView.cpp
Reply

Use magic Report

0

Threads

45

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-5-19 07:15:01
| Show all posts
extern g_sCtrldata * g_ContrldataHipA; // Global variable declaration
Reply

Use magic Report

0

Threads

32

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 Invalid IP Address

Post time: 2020-5-19 18:30:02
| Show all posts
Add a struct before g_sCtrldata * g_ContrldataHipA;
struct g_sCtrldata * g_ContrldataHipA;

Then when used in other files:
extern struct g_sCtrldata * g_ContrldataHipA;

Or you can use typedef to define the structure weight.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-31 20:30:01
| Show all posts
Define the structure in the .h file and use the structure to define the variable in the corresponding .cpp file; then #include .h where the variable is used and define the external variable with extern struct.

In StdAfx.h:
struct g_sCtrldata
{
struct g_sCtrldata * pNext;
struct g_sCtrldata * head;
double x;
double y;
double dy;
double ddy;
}; // Structure definition
In StdAfx.cpp
struct bg_sCtrldata * g_ContrldataHipA; // Global variable declaration

In the .cpp file to be used
#include "StdAfx.h"
extern struct bg_sCtrldata * g_ContrldataHipA;
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-6-2 20:30:02
| Show all posts
Thank you all for your support and help.
Also ask upstairs:
In StdAfx.cpp
struct bg_sCtrldata * g_ContrldataHipA;//Global variable declaration
Why does "bg_sCtrldata" appear? The structure name defined in the previous .h is "g_sCtrldata". How did that "b" come from? ?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-14 15:00:01
| Show all posts
It's because you hit an extra b in your question. I didn't pay attention to copy it directly. It should be g_sCtrldata.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list