开发者

Problem with an array of pointers

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开发者_如何学C_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


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.


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.


You need to dereference the CString pointer:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜