简体   繁体   English

结构值未保留,值更改为-858993460

[英]Stucture values not staying, values changed to -858993460

I'm clearly doing something wrong or forgetting something. 我显然在做错事或忘记了事。 I have defined a structure in a header file with four variables. 我已经在带有四个变量的头文件中定义了一个结构。 I then assign values to those variables in a function located in a different .cpp then I try and take the new values from the structure and assign to different variable in another function. 然后,我将值分配给位于不同.cpp中的函数中的那些变量,然后尝试从结构中获取新值并分配给另一个函数中的不同变量。 Problem is, I'm able to assign values to the variables in the structure but when I try and transfer those values to other variables they become something like -858993460(this is according to the debugger and watching the threads). 问题是,我能够为结构中的变量分配值,但是当我尝试将这些值转移给其他变量时,它们变成类似-858993460(这是根据调试器并监视线程的)。 How do I fix this? 我该如何解决?

Structure Defined with it's function(even though not it's not currently be used) 用它的功能定义的结构(即使不是当前不使用)

struct Setting {
    int Max;
    int Min;
    int Sample;
    int Points;
} Set1, Set2;

** Assigning Values to Structure variables** **为结构变量分配值**

void Settings::OnBnClickSet() {
    GetDlgItemText(ID_Points,str4);
    CW2A buf3((LPCWSTR)str4);
    Serial Value;
    Value.Set1.Points = atoi(buf3);
}

Attempting to transfer those values to another variable 尝试将这些值转移到另一个变量

bool Serial::Temperature(CString) {
    int Max,Min,SampleTime,Points;
    Max = Set1.Max;
    Min = Set1.Min;
    SampleTime = Set1.Sample;
    Points = Set1.Points;
}

You're setting values on a local (automatic) variable. 您正在本地(自动)变量上设置值。 Those changes are local to the function in which the variable is declared ( OnBnClickSet() ). 这些更改对于声明变量的函数( OnBnClickSet() )是局部的。

If you want to use a single instance of Serial , you will need to either pass it in to the OnBnclickSet() function, or make it available via some other means (using a global variable or singleton for instance). 如果要使用Serial的单个实例,则需要将其传递到OnBnclickSet()函数,或通过其他方式(例如,使用全局变量或单例)使其可用。

void Settings::OnBnClickSet() {
    GetDlgItemText(ID_Points,str4);
    CW2A buf3((LPCWSTR)str4);

    // This creates a Serial object with automatic storage duration
    Serial Value;

    // You're setting the value on the local (automatic) variable.
    Value.Set1.Points = atoi(buf3);

    // Value will now go out of scope, all changes to it were
    // local to this function
}


bool Serial::Temperature(CString) {
    int Max,Min,SampleTime,Points;

    // The member (?) variable Set1 was never modified on this instance of `Serial`
    Max = Set1.Max;
    Min = Set1.Min;
    SampleTime = Set1.Sample;
    Points = Set1.Points;
}

but when I try and transfer those values to other variables they become something like -858993460 但是当我尝试将这些值传递给其他变量时,它们变成类似-858993460的形式

That's a Magic Number. 那是个魔术数字。 Convert it to Hex to get 0xcccccccc. 将其转换为十六进制以获取0xcccccccc。 Which is the value that's used to initialize variables when you build your program with the Debug configuration settings. 当使用“调试”配置设置构建程序时,该值用于初始化变量。

So whenever you see it back in the debugger, you go "Ah! I'm using an uninitialized variable!" 因此,每当您在调试器中看到它时,您就去“啊!我正在使用一个未初始化的变量!” All we can really see from the snippet is that your Set1 struct never got initialized. 从片段中我们真正看到的就是您的Set1结构从未初始化。 Focus on the code that's supposed to do this, with non-zero odds that you just forgot to write that code or are using the wrong structure object. 将精力集中在应该执行此操作的代码上,因为您忘记编写该代码或使用了错误的结构对象而导致的非零可能性。

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

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