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

发布于 2024-09-17 18:29:16 字数 807 浏览 3 评论 0原文

我有一个全局多维数组,在表单中使用 g_iAllData[MAX_LEN][MAX_WIDTH] 。当我在函数中写入它时: g_iAllData[iRow][iColumn]= iByte_Count; 我可以在监视窗口中看到它的内容没有被更改。如果我将数组放入函数中,它就可以正常工作。

我有什么遗漏的吗?我在 Form1.h 文件顶部的 #include 之后将其声明为全局的。我有多个通过按下按钮来调用的函数,我需要在每个函数中写入和读取数组。将其保留为全局而不是将其传递给每个函数会更容易。

更新代码:

ProgramName.cpp

#include "stdafx.h"
#include "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)

g_iAllData[iRow][iColumn]= iByte_Count;

I have a global multidimensional array, g_iAllData[MAX_LEN][MAX_WIDTH] being used in a Form. When I write to it in a function: g_iAllData[iRow][iColumn]= iByte_Count; I can see in a Watch Window that it's contents are not being changed. 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. 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

#include "stdafx.h"
#include "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)

g_iAllData[iRow][iColumn]= iByte_Count;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虫児飞 2024-09-24 18:29:16

您的代码示例确实证实了您的变量声明存在问题。

正如 @Graham 所暗示的,定义全局变量的正确方法是:

  • 在 cpp 文件中定义变量,
  • 在头文件中将变量声明为 extern

//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];    

这样,链接器将在一个编译单元中找到全局变量的定义,并且在 #include 标头的所有其他编译单元中,它将能够将 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:

  • define the variable in a cpp file
  • declare the variable as extern in a header file

I.e.

//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.

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.

帅冕 2024-09-24 18:29:16

您是否也在其他文件中包含 Form1.h ?如果是这样,您需要在其他文件中使用“extern”。

Are you including Form1.h in other files too? If so, you need to use 'extern' in the other files.

你曾走过我的故事 2024-09-24 18:29:16

如果您在头文件中定义了数组(如代码所示),那么每次您在翻译单元中包含该标头时,您就有效地定义了数组的单独“副本”。这甚至不应该编译,因为您正在定义同一外部对象的多个实例(违反 ODR)。

如果您设法以某种方式编译它(如何?),那么就无法知道您正在修改哪个 g_iAllData 实例以及调试器在监视窗口中显示哪个实例。可能是不同的实例,这就是您看不到变化的原因。

不应在头文件中定义具有外部链接的对象。你最好重新考虑你的方法。

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).

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文