简体   繁体   English

在 c++ 中使 typedef 结构外部可用

[英]Making a typedef struct externally available in c++

I think I'm doing something stupid, but I don't quite know what.我想我在做一些愚蠢的事情,但我不太清楚是什么。 I'm trying to rewrite a fairly simple library I have that is reading in some variables from a text file and then shares them with some other source files for use elsewhere.我正在尝试重写我拥有的一个相当简单的库,该库正在从文本文件中读取一些变量,然后与其他一些源文件共享它们以在其他地方使用。 When I wanted to pass a double I was doing this:当我想通过双倍时,我正在这样做:

#if !defined(SharedVariables_h)
#define SharedVariables_h

#include <vector>

extern double coeff_A;

#endif

and then doing this in both the source file where it was defined and the other source files where I want to use it:然后在定义它的源文件和我想使用它的其他源文件中执行此操作:

#include "SharedVariables.h"

to make coeff_A available in the new source file.使coeff_A在新的源文件中可用。

But then my model got more complicated and I was using more coefficients so I decided to store them in a structure and pass that structure using extern instead.但是后来我的 model 变得更加复杂,我使用了更多的系数,所以我决定将它们存储在一个结构中并使用extern传递该结构。 I created a new header file for the structure ( CParameters.h ):我为结构( CParameters.h )创建了一个新的 header 文件:

#pragma once
#if !defined(CParameters_h)
#define CParameters_h

typedef struct
{
    int         coeff_A;
    int         coeff_B;
    double      coeff_C;
    double      coeff_D;

} SGlobalParameterData;


#endif

and then defined the struct like this:然后像这样定义结构:

#if !defined(SharedVariables_h)
#define SharedVariables_h

#include "CParameters.h"
#include <vector>

extern SGlobalParameterData globalParameters;

#endif

In the first source file where I read in all the values from the data files and using the values I've replaced:在我从数据文件中读取所有值并使用已替换的值的第一个源文件中:

#include "SharedVariables.h"


double coeff_A;
...

with

#include "SharedVariables.h"


SGlobalParameterData globalParameters;

globalParameters = readProperties(prefsFile);

....

In the second source file which also needs to use some of these variables I've added #include "SharedVariables.h" and then the IDE and compiler let me easily access globalParameters.coeff_A , globalParameters.coeff_B , etc.在也需要使用其中一些变量的第二个源文件中,我添加了#include "SharedVariables.h"然后 IDE 和编译器让我可以轻松访问globalParameters.coeff_AglobalParameters.coeff_B等。

I thought this should work and everything compiles without any errors, but it's not actually passing the values read into globalParameters to the second source file.我认为这应该可以工作,并且一切都可以编译而没有任何错误,但它实际上并没有将读入globalParameters的值传递给第二个源文件。 Everything is just zero when I'm debugging the code.当我调试代码时,一切都为零。

So there are really two questions: 1) Is it possible to share structs using extern like for double , and 2) if it is, what have I done wrong.所以实际上有两个问题:1)是否可以使用extern like for double来共享结构,以及 2)如果是,我做错了什么。

Hopefully someone can point out my mistake.希望有人能指出我的错误。

Edit编辑

I've added in an line of code in my example to show the function that assigns the values to the struct.我在示例中添加了一行代码,以显示将值分配给结构的 function。 This is working and the values read from the prefsFile are available and correct in the f1rst source file, but these values are not passed to source file 2. I'm going to do some more testing because it appears I may have made some other mistake elsewhere.这是有效的,从prefsFile读取的值在第一个源文件中可用且正确,但这些值未传递到源文件 2。我将进行更多测试,因为看起来我可能犯了其他错误别处。

Your confusion stems from the use of typedef and mixing of declarations and definitions.您的困惑源于typedef的使用以及声明和定义的混合。

In C++ typedef isn't needed to define a struct type and is only confusing.在 C++ 中,不需要typedef来定义struct类型,只会令人困惑。

typedef struct {... } SGlobalParameterData; - this declares a type SGlobalParameterData that is a struct . - 这声明了一个类型SGlobalParameterData ,它是一个struct It defines no instance of the struct.它没有定义结构的实例。

struct SGlobalParameterData {... }; - same thing, declares a type SGlobalParameterData that is a struct . - 同样的,声明一个类型SGlobalParameterData是一个struct

struct {... } globalParameterData; - this defines an object globalParameterData of an unnamed struct type. - 这定义了一个未命名struct类型的 object globalParameterData

struct SGlobalParameterData {... } globalParameters; - this defines an object globalParameters of an struct type called SGlobalParameterData . - 这定义了一个名为globalParametersstruct类型的SGlobalParameterData

So... the following might be most readable/clean IMHO:所以......以下可能是最易读/干净的恕我直言:

In the header:在 header 中:

struct SGlobalParameterData   // type declaration
{
    int         coeff_A;
    int         coeff_B;
    double      coeff_C;
    double      coeff_D;
};

extern SGlobalParameterData globalParameters; // global instance *declaration*

In one of source files:在源文件之一中:

#include "the header above"

SGlobalParameterData globalParameters; // global instance *definition*

In other source files:在其他源文件中:

#include "the header above"

// globalParameters is already visible and usable

Of course you have to compile all source files and link them together into an executable.当然,您必须编译所有源文件并将它们链接在一起成为可执行文件。

  1. Is it possible to share structs using extern like for double是否可以使用 extern like for double 共享结构

Yes.是的。

  1. if it is, what have I done wrong.如果是,我做错了什么。

Something that you haven't shown to us.你没有向我们展示的东西。

Everything is just zero一切都只是零

This is to be expected because variables with static storage are zero-initialised, and you never modify globalParameters in the example.这是意料之中的,因为具有 static 存储的变量是零初始化的,并且您永远不会在示例中修改globalParameters

Here is your code running.这是您运行的代码。 Only change I made was to add some output, and actually set some other value than zero.我所做的唯一更改是添加一些 output,实际上设置了一些其他值而不是零。 That non-zero value works as expected: https://wandbox.org/permlink/CasO8c68W3FmBsQH该非零值按预期工作: https://wandbox.org/permlink/CasO8c68W3FmBsQH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM