Search code examples
headerc++-cliorganization

Separating click event from .h (designer code) to .cpp file


I've run into an organization problem with windows application form that I need some help with. Right now the code is done in microsoft studio 2010 in .net 4.0 in c++. The header file for the GUI application is not organized which can potentially lead to 10,000 or more lines of code, which makes it very difficult to read.

I was trying to separate the click event implementation into a .cpp file that included the "Form1.h".

private:
    System::Void sIToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e){
        switch(this->USCustomaryFlg){
        case true:
            this->sIToolStripMenuItem->Checked = true;
            this->mnuUSMetric->Checked = false;
            this->USCustomaryFlg = false;
            cout << "SI flag is now true." << endl;
            break;
        case false:
            break;
        }
    }

into this form in the .cpp file of Form1:

System::Void mnuUSMetric_Click(System::Object^  sender, System::EventArgs^  e){
    switch(this->USCustomaryFlg){
    case true:
        cout << "USCustomaryFlg is now false." << endl;
        break;
    case false:
        this->mnuUSMetric->Checked = true;
        this->USCustomaryFlg = true;
        this->sIToolStripMenuItem->Checked = false;
        cout << "USCustomaryFlg is now true." << endl;
        break;
    }
}

The error I've been receiving are just a sample of what I've been getting:

Error 2 error C2355: 'this' : can only be referenced inside non-static member functions E:\Summer 2011\Engineer Software\GUItest\GUItest\Form1.cpp 16

Error 4 error C2355: 'this' : can only be referenced inside non-static member functions E:\Summer 2011\Engineer Software\GUItest\GUItest\Form1.cpp 21

Error 3 error C2227: left of '->USCustomaryFlg' must point to class/struct/union/generic type E:\Summer 2011\Engineer Software\GUItest\GUItest\Form1.cpp 16

Error 8 error C2227: left of '->USCustomaryFlg' must point to class/struct/union/generic type E:\Summer 2011\Engineer Software\GUItest\GUItest\Form1.cpp 22

Any thoughts on this?


Solution

  • If your C++ method is defined (i.e. has its body written) separately from its declaration (i.e. it's signature inside of class), you need to prefix method definition with class name.

    E.g. if your class is named MyForm the definition should be

    System::Void MyForm::mnuUSMetric_Click(System::Object^  sender, System::EventArgs^  e){
        ...
    }