简体   繁体   English

全局多维数组未写入[vs c ++]

[英]Global multidimensional array not being written to [vs c++]

I have a global multidimensional array, g_iAllData[MAX_LEN][MAX_WIDTH] being used in a Form. 我有一个全局多维数组,在表单中使用g_iAllData[MAX_LEN][MAX_WIDTH] When I write to it in a function: g_iAllData[iRow][iColumn]= iByte_Count; 当我在函数中写入它时: g_iAllData[iRow][iColumn]= iByte_Count; I can see in a Watch Window that it's contents are not being changed. 我可以在Watch Window中看到它的内容没有被更改。 If I put the array in the function, it works fine. 如果我把数组放在函数中,它工作正常。

Is there something I'm missing? 有什么我想念的吗? I am declaring it as global after my #include 's at the top of the Form1.h file. 我在Form1.h文件顶部的#include之后声明它是全局的。 I have multiple functions that are called by buttons being pressed and I need to write and read from the array in each function. 我有多个按下按钮调用的函数,我需要在每个函数中写入和读取数组。 It would be easier to keep it as global instead of passing it to each function. 将它保持为全局而不是将其传递给每个函数会更容易。

UPDATE code: 更新代码:

ProgramName.cpp ProgramName.cpp

#include "stdafx.h"
#include "Form1.h"  

Form1.h Form1.h

#include <iostream>
#include <string>
...
#pragma once  

const int MAX_LEN = 4033;
const int MAX_WIDTH = 21;
int g_iAllData[MAX_LEN][MAX_WIDTH];    
...
namespace ProgramName{
// later on
ReadFile();  

void ReadFile(void) void ReadFile(void)

g_iAllData[iRow][iColumn]= iByte_Count;

Are you including Form1.h in other files too? 你是否也在其他文件中包含Form1.h? If so, you need to use 'extern' in the other files. 如果是这样,您需要在其他文件中使用“extern”。

Your code sample really confirms that you have a problem with your variable declaration. 您的代码示例确实证实您的变量声明存在问题。

As @Graham hinted, the proper way to define globals is: 正如@Graham暗示的那样,定义全局变量的正确方法是:

  • define the variable in a cpp file 在cpp文件中定义变量
  • declare the variable as extern in a header file 在头文件中将变量声明为extern

Ie

//ProgramName.cpp

#include "stdafx.h"
#include "Form1.h"  

int g_iAllData[MAX_LEN][MAX_WIDTH];    

//Form1.h

#include <iostream>
#include <string>
...
#pragma once  

const int MAX_LEN = 4033;
const int MAX_WIDTH = 21;
extern int g_iAllData[MAX_LEN][MAX_WIDTH];    

This way the linker will find the definition of the global variable in exactly one compilation unit, and in all other compilation units which #include the header, it will be able to link the extern declarations to the correct variable definition. 这样链接器将在一个编译单元中找到全局变量的定义,并且在#include头部的所有其他编译单元中,它将能够将extern声明链接到正确的变量定义。

Barring this, strange things may happen: you may get cryptic linker error messages complaining about multiple variable definitions, or you might even get multiple distinct variables in your app instead of one global variable - the latter explains why your method doesn't seem to change the contents of your variable. 除此之外,可能会发生奇怪的事情:您可能会收到抱怨多个变量定义的神秘链接器错误消息,或者您甚至可能在应用程序中获得多个不同的变量而不是一个全局变量 - 后者解释了为什么您的方法似乎没有改变变量的内容。

If you defined your array in header file (as you code shows), then every time you include that header in a translation unit, you effectively define a separate "copy" of your array. 如果您在头文件中定义了数组(如代码所示),那么每次在翻译单元中包含该标题时,您都可以有效地定义数组的单独“副本”。 This is not even supposed to compile, since you are defining multiple instances of the same external object (violation of ODR). 这甚至不应该编译,因为您正在定义同一外部对象的多个实例(违反ODR)。

If you managed to compile it somehow (how?), then there's no way to say which instance of g_iAllData you are modifying and which instance the debugger shows you in the watch window. 如果你设法以某种方式编译它(如何?),那么就没有办法说明你正在修改哪个g_iAllData实例以及调试器在监视窗口中显示哪个实例。 Could be different instances, which is why you don't see the change. 可能是不同的实例,这就是您没有看到更改的原因。

Objects with external linkage should not be defined in header files. 不应在头文件中定义具有外部链接的对象。 You better reconsider your approach. 你最好重新考虑你的方法。

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

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