如何使用我的事件函数定义一个单独的.cpp文件在Windows窗体?(How to use a se

2019-07-29 03:31发布

我在定义Windows窗体中我的C ++事件函数的麻烦。

我想定义我的事件功能:在一个单独的.cpp文件,而不是做所有的函数定义在.h文件中这已经充满了生成的代码Windows窗体GUI窗口的形式(例如点击按钮)。

我试过Form1.h类中这样做,宣言:

private: System::Void ganttBar1_Paint
(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e);

这是Form1.cpp类中的定义:

#include "Form1.h"

System::Void Form1::ganttBar1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e)
{
    // Definition
}

当我这样做,我得到编译器错误在.cpp文件中说,它不是一个类或命名空间名称。

我能做些什么来得到seprate文件的情况下,函数的定义和声明?

我只是愚蠢,在这里失去了一些东西还是我必须用另一种方法比C ++标准做这些事情?

Answer 1:

您的类定义是最有可能的一些命名空间(我将使用内部Project1作为占位符):

#pragma once

namespace Project1
{
    ref class Form1 : public System::Windows::Forms::Form
    {
        // ...
    };
}

因此,你的定义必须还有:

#include "Form1.h"

namespace Project1
{
    void Form1::ganttBar1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
    {
        // definition
    }
}

要么

#include "Form1.h"

void Project1::Form1::ganttBar1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
    // definition
}


文章来源: How to use a separate .cpp file for my event function definitions in windows forms?