header - 将点击事件从 .h(设计器代码)分离到 .cpp 文件

标签 header c++-cli organization

我遇到了 Windows 申请表的组织问题,需要一些帮助。现在代码是在 microsoft studio 2010 的 .net 4.0 中用 c++ 完成的。 GUI 应用程序的头文件未组织,这可能会导致 10,000 行或更多行代码,这使得阅读变得非常困难。

我试图将点击事件实现分离到包含“Form1.h”的 .cpp 文件中。

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;
        }
    }

在Form1的.cpp文件中插入此表单:

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;
    }
}

我收到的错误只是我收到的错误的一个示例:

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

对此有什么想法吗?

最佳答案

如果您的 C++ 方法是与其声明(即它在类内部的签名)分开定义的(即编写其主体),则需要在方法定义前加上前缀类(class)名称。

例如如果您的类名为 MyForm ,则定义应为

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

关于header - 将点击事件从 .h(设计器代码)分离到 .cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6419385/

相关文章:

c++ - 为什么编译器无法在 .cpp 文件中找到方法定义

.net - 将标准 C++ 嵌入到 .NET 应用程序中

javascript - 在 Rails 中使用 Google 的可视化组织结构图

python - 为不同版本的 Python 维护不同版本代码库的工作流

css - Wordpress:标题中的小部件在移动设备中流向 Logo

c++ - 在 C++ 中定义头文件

php - 使用 header.php 页面时,我将如何使用 PHP 定义导航栏的 "active"选项卡

c# - 如何从 c# dll、c++/cli 调用的 dll 返回字节数组、uint 值

c# - 将数据从 C++ 传递到 C# - 映射文件与 CLI+UnmanagedMemoryStream

cmake - 如何处理 C++ 库中的资源