指针数组的问题
语言: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
而不是
使用
因为你已经有一定程度的间接性了。
关于这一点:
我不确定您是否知道您在那里设置的条件意味着正确的条件,因为这就是逗号运算符的工作原理。您应该保留左边的一个,因为当
j << 时它永远不会为假。 4
表达式就对了,或者使用 &&运营商更清楚。我假设您使用 Visual Studio 进行 MFC 编程,因此我建议在该行设置一个断点并确保您的数组已正确初始化。如果是,那么问题出在其他地方。
Instead of
use
As you already have a level of indirection there.
About this:
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.
应该是
虽然,看看你的 for 循环,以及你的索引
i
和j
,我不确定你想要完成什么。should be
Although, looking at your for-loop, and your indices
i
andj
, I am unsure what you are trying to accomplish.您需要取消引用 CString 指针:
You need to dereference the CString pointer:
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.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 toCString
. 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
), comparesi
to 2010, throws the result away, comparesj
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.
不要
使用
地址 (
&var[j]
) 或取消引用 (*var[j]
)。只需使用 var[j] 即可。Instead of
use
Do not use address-of (
&var[j]
) or dereference (*var[j]
). Just usevar[j]
.