tags:

views:

125

answers:

2

Hi I have just started using C++ today, and I am working on checkboxes. I have tried using CheckBox1->Checked in an if statement or whatever, but it isn't working.

The error is:

Error 2 error C2227: left of '->Checked' must point to class/struct/union/generic type

EDIT: The Code is:

    void function ()
{
 if (1001->Checked)
 {
             Sleep(2000);
 }
}
+5  A: 

Without seeing some of your code, it's very difficult to offer targeted assistance.

However, that error message usually comes about because the item you're de-referencing is not a pointer.

Check to ensure it's of the correct type. It should be something along the lines of:

tCheckBox *CheckBox1;

One possibility is that you've declared it not as a pointer to the checkbox but as a checkbox itself:

tCheckBox CheckBox1;

Note the lack of the asterisk there that would otherwise mark it as a pointer. In that case, you would use CheckBox1.Checked rather than CheckBox1->Checked, if it's allowed by the framework (this isn't standard C++ since that beast has no concept of GUI libraries).

If that doesn't help, please post the code so we can offer better suggestions.

Update:

if (1001->Checked) ?????

1001 is not a pointer - it's not a variable of any description, it's an integer constant.

You need to declare and use a variable of some description. First step is, I think, to read up on the documentation for your framework and/or get some sample code that does compile and work, basing your initial work of that.

paxdiablo
I'm guessing 1001 is the resource IDC of the control in MFC.
Martin Beckett
+1  A: 

Use CButton::GetCheck() to determine the state of the checkbox - like so...

CButton* pButton = (CButton*) GetDlgItem(IDC_CHECKBOX_RESOURCE_ID);

if ( BST_CHECKED == pButton->GetCheck() )
{
  // button is checked
}
mocj
One of the low points of MFC is that it doesn't know the type of a window. You must typecast the results of GetDlgItem to CButton*, because otherwise it won't assign it to a button pointer.
Mark Ransom
Yep, I knew I was forgetting something. Thanks.
mocj