指针数组的问题

发布于 2024-11-15 02:38:10 字数 1101 浏览 1 评论 0原文

语言: C++、MFC

问题: 我试图向函数传递一些指向数组中包含的变量的指针,但编译器似乎不同意如何传递我正在做。这是我的代码:

头文件:

CString m_strTop;
CString m_strLeft;
CString m_strRight;
CString m_strBottom;

CString *var[4];

源文件: Constructor()

CString *var[4] = {
  &m_strTop
, &m_strLeft
, &m_strRight
, &m_strBottom
};

源文件: DoDataExchange()

void FSC_3DPersp::DoDataExchange(CDataExchange* pDX)
{
   CSAPrefsSubDlg::DoDataExchange(pDX);

   for(int i = 2001, j = 0; i <= 2004, j < 4; i++, j++)
   {
       DDX_Text(pDX, i, &var[j]); // 'i' is the ID of the textbox
   }
} 

-- DDX_Text 期望的 --

void AFXAPI DDX_Text(
   CDataExchange* pDX,
   int nIDC,
   CString& value 
);

我想以这种方式进行 DataExchange,因为在我的几个文件中,我有超过 75 个变量,并且使用循环显着压缩了代码并简化了事情。

我知道我遇到的问题是我只是向 DDX_Text 提供了错误的参数,但我知道它需要 CStrings。但是,我很确定我没有正确引用它们。

任何帮助将不胜感激!

〜乔恩

Language: C++, MFC

Problem: I am attempting to pass a function some pointers to variables that are contained within an array, but the compiler doesn't seem to agree with how I'm doing it. Here is my code:

Header File:

CString m_strTop;
CString m_strLeft;
CString m_strRight;
CString m_strBottom;

CString *var[4];

Source File:
Constructor()

CString *var[4] = {
  &m_strTop
, &m_strLeft
, &m_strRight
, &m_strBottom
};

Source File:
DoDataExchange()

void FSC_3DPersp::DoDataExchange(CDataExchange* pDX)
{
   CSAPrefsSubDlg::DoDataExchange(pDX);

   for(int i = 2001, j = 0; i <= 2004, j < 4; i++, j++)
   {
       DDX_Text(pDX, i, &var[j]); // 'i' is the ID of the textbox
   }
} 

-- What DDX_Text expects --

void AFXAPI DDX_Text(
   CDataExchange* pDX,
   int nIDC,
   CString& value 
);

I wanted to do my DataExchange this way because in several of my files, I have upwards of 75 variables, and using a loop significantly condenses the code, and simplifies things.

I know that the problem I'm having is that I'm just feeding DDX_Text the wrong parameters, but I know that it takes CStrings. However, I'm pretty sure I'm not referencing them correctly.

Any help would be greatly appreciated!

~ Jon

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

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

发布评论

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

评论(6

浅紫色的梦幻 2024-11-22 02:38:10

而不是

DDX_Text(pDX, i, &var[j]);

使用

DDX_Text(pDX, i, *(var[j]));

因为你已经有一定程度的间接性了。

关于这一点:

for(int i = 2001, j = 0; i <= 2004, j < 4; i++, j++)

我不确定您是否知道您在那里设置的条件意味着正确的条件,因为这就是逗号运算符的工作原理。您应该保留左边的一个,因为当 j << 时它永远不会为假。 4 表达式就对了,或者使用 &&运营商更清楚。

我假设您使用 Visual Studio 进行 MFC 编程,因此我建议在该行设置一个断点并确保您的数组已正确初始化。如果是,那么问题出在其他地方。

Instead of

DDX_Text(pDX, i, &var[j]);

use

DDX_Text(pDX, i, *(var[j]));

As you already have a level of indirection there.

About this:

for(int i = 2001, j = 0; i <= 2004, j < 4; i++, j++)

I'm not sure if you are aware that the condition you set there will mean the right one, because that's how comma operator works. You should either leave the left one out as it will never get to be false when the j < 4 expression is right to it, or use the && operator to be more clear.

I assume you use Visual Studio to do MFC programming, so I suggest set a breakpoint on that line and make sure that your array is initialized correctly. If it is, then the problem is somewhere else.

故人如初 2024-11-22 02:38:10
DDX_Text(pDX, i, &var[j]); // 'i' is the ID of the textbox

应该是

DDX_Text(pDX, i, *var[j]); // 'i' is the ID of the textbox

虽然,看看你的 for 循环,以及你的索引 ij,我不确定你想要完成什么。

DDX_Text(pDX, i, &var[j]); // 'i' is the ID of the textbox

should be

DDX_Text(pDX, i, *var[j]); // 'i' is the ID of the textbox

Although, looking at your for-loop, and your indices i and j, I am unsure what you are trying to accomplish.

∞觅青森が 2024-11-22 02:38:10

您需要取消引用 CString 指针:

DDX_Text(pDX, i, *var[j])

You need to dereference the CString pointer:

DDX_Text(pDX, i, *var[j])
路还长,别太狂 2024-11-22 02:38:10

DDX_Text(pDX, i, &var[j]); 正在发送一个指针的地址,一个 CString **。

您的 DDX_Text 函数要求提供对该值的引用。

尝试使用 DDX_Text(pDX, i, *var[j]); 代替。

DDX_Text(pDX, i, &var[j]); is sending the address of a pointer, a CString **.

Your DDX_Text function is asking for a reference to the value.

Try DDX_Text(pDX, i, *var[j]); instead.

花开柳相依 2024-11-22 02:38:10

DDX_Text 需要对 CString 的引用。但是,&var[j] 生成一个指针 (CString**)。您应该使用 *var[j] 调用它(取消引用指针) - 即 DDX_Text(pDX, i, *var[j]);

编辑:
您的循环可能没有达到您的预期。 for 循环条件 (i <= 2010, j <4) 将 i 与 2010 进行比较,丢弃结果,然后进行比较j 到 4 并使用该结果。如果要组合条件,请使用 &&(逻辑 AND)、||(逻辑 OR)或 !(逻辑 NOT) 。

不过,指数似乎还不错。

DDX_Text expects a reference to CString. However, &var[j] yields a pointer (CString**). You should call it with *var[j] (dereference the pointer) - i.e. DDX_Text(pDX, i, *var[j]);.

edit:
Your loop probably does not do what you expect. The for-loop condition (i <= 2010, j < 4), compares i to 2010, throws the result away, compares j to 4 and uses that result. If you want to combine conditions, use && (logical AND), || (logical OR) or ! (logical NOT).

However, the indices seem to be alright.

半枫 2024-11-22 02:38:10

不要

DDX_Text(pDX, i, &var[j]);

使用

DDX_Text(pDX, i, var[j]);

地址 (&var[j]) 或取消引用 (*var[j])。只需使用 var[j] 即可。

Instead of

DDX_Text(pDX, i, &var[j]);

use

DDX_Text(pDX, i, var[j]);

Do not use address-of (&var[j]) or dereference (*var[j]). Just use var[j].

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