How to use a separate .cpp file for my event function definitions in windows forms?

ぃ、小莉子 提交于 2019-12-13 12:12:00

问题


I'm having trouble defining my C++ event functions in windows forms.

I want to define my event functions (example: button click) in a separate .cpp file instead of doing all the function definitions in the windows forms .h file that's already full of generated code for the windows forms GUI.

I tried doing this, Declaration inside the Form1.h class:

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

And this is the definition inside Form1.cpp class:

#include "Form1.h"

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

When I do this i get compiler errors in the .cpp file saying that it's not a class or namespace name.

What can i do to get the definitions and declarations of the event functions in seprate files?

Am I just being stupid and missing something here or do i have to do these things in another way than the C++ standard?


回答1:


Your class definition is most likely inside of some namespace (I'll use Project1 as a placeholder):

#pragma once

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

Consequently, your definition needs to be as well:

#include "Form1.h"

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

or

#include "Form1.h"

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


来源:https://stackoverflow.com/questions/10305922/how-to-use-a-separate-cpp-file-for-my-event-function-definitions-in-windows-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!