Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;结构数组赢形式损坏_C++_C++ Cli - Fatal编程技术网

C++ C++;结构数组赢形式损坏

C++ C++;结构数组赢形式损坏,c++,c++-cli,C++,C++ Cli,一整天都被困在这个问题上。当程序在main的第1行启动时,statstruct中的数据在第一秒钟就被破坏了。我不知道为什么当我试图将statsStruct声明为全局时会变成这样。 编辑:只编译statsstruct中的所有值,文本已损坏,值为323232 extern attributes statsStruct[]; statsStruct在多个cpp的头文件中是外部的,但Iv删除了所有源代码,直到只剩下statsStruct,但我无法使其成为全局的。当我在一个函数中声明它时,它的值很好,但

一整天都被困在这个问题上。当程序在main的第1行启动时,statstruct中的数据在第一秒钟就被破坏了。我不知道为什么当我试图将statsStruct声明为全局时会变成这样。 编辑:只编译statsstruct中的所有值,文本已损坏,值为323232

extern attributes statsStruct[];
statsStruct在多个cpp的头文件中是外部的,但Iv删除了所有源代码,直到只剩下statsStruct,但我无法使其成为全局的。当我在一个函数中声明它时,它的值很好,但我需要它跨多个文件全局声明,并让它共享相同的值

 // tess.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include "test.h"
using namespace tess;
struct attributes{
   std::string stat;
   int amount;  
};  
attributes statsStruct[] = {{"Acc",0},
                        {"Cri",0},
                        {"Cr",0},
                        {"Crit",0},
                        {"Cr",0},
                        {"Ev",0},
                        {"An",0}};

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
   // Enabling Windows XP visual effects before any controls are created
   Application::EnableVisualStyles();
   Application::SetCompatibleTextRenderingDefault(false); 

   // Create the main window and run it
   Application::Run(gcnew Form1());
   return 0;
}
//tess.cpp:主项目文件。
#包括“stdafx.h”
#包括“表格1.h”
#包括“test.h”
使用名称空间tess;
结构属性{
std::字符串stat;
整数金额;
};  
属性statstruct[]={{“Acc”,0},
{“Cri”,0},
{“Cr”,0},
{“Crit”,0},
{“Cr”,0},
{“Ev”,0},
{“An”,0};
[属性]
int main(数组^args)
{
//在创建任何控件之前启用Windows XP视觉效果
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//创建主窗口并运行它
应用程序::运行(gcnewform1());
返回0;
}

我想我是因为这件事得到了一次机会,而不是偶然。这是由项目中的编译器设置引起的。要解决此问题,请在“解决方案资源管理器”窗口的“属性”和“常规”中右键单击项目。将公共语言运行时支持设置从
/clr:pure
更改为
/clr

听到这个解释,我需要挥挥手。在/clr:pure生效的情况下,编译器只允许生成纯IL,而不允许生成机器代码。问题是,CLR不支持全局变量。编译器必须做一些特技来模拟您的attributes[]数组,一个非托管数组,并正确初始化它。这似乎足以混淆调试器,它会将您带到“statsArray”的垫片,而不是实际的statsArray数据。您确实会在数组元素中看到垃圾。需要一个额外的指针解引用,然后调试器忘记了这样做


如果使用非托管声明,请始终使用有效的
/clr
进行编译。

您应该指出如何判断数组已损坏。可能是您读取数组的代码错误。使用调试器单步执行,在main中的第一个点处设置断点,然后查看它。我也在假设使用它的函数中研究过它,它也出了问题,所以我相信它的声明方式可能是什么?这是一个新的胜利形式的东西。没有接口代码,代码就没有问题。似乎有人知道:(为什么在CLR应用程序中使用本机类型?因为应用程序的其余部分在接口之前就使用这些类型进行了编码。谢谢,这让我为此疯狂地浪费了数天。它是有效的:)