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