C++ cli 在Visual C++ 2010中从头调用函数 我在Visual C++ 2010中编程。我有一个例子。h example.cpp和Form1.h。 基本上,我粘贴了一些代码。我无法在example.h文件中包含Form1.h,不知道为什么。但主要的问题是我如何调用example.cpp中格式为1.h的Test?语法是什么?有可能这样做吗

C++ cli 在Visual C++ 2010中从头调用函数 我在Visual C++ 2010中编程。我有一个例子。h example.cpp和Form1.h。 基本上,我粘贴了一些代码。我无法在example.h文件中包含Form1.h,不知道为什么。但主要的问题是我如何调用example.cpp中格式为1.h的Test?语法是什么?有可能这样做吗,c++-cli,C++ Cli,我的表格1.h #include "example.h" public ref class Form1 : public System::Windows::Forms::Form { public: void Test(void) { // Does something } } 我的例子.cpp #include "example.h" #include "Form1.h" Test(); // would like to call

我的表格1.h

#include "example.h" 
public ref class Form1 : public System::Windows::Forms::Form
{
    public: void Test(void)
    {
              // Does something
    }
}
我的例子.cpp

#include "example.h"
#include "Form1.h"

Test();    // would like to call Test from here. 

这里有两个问题:

必须从另一个函数内部调用函数。您当前在example.cpp文件中的代码无效,因为您试图在全局范围内调用测试函数

让它看起来像这样:

int main()
{
    Test();

    return 0;
}

这也解决了你没有一个主函数的问题,它是任何C++应用程序的入口点。 更一般地说,我强烈建议使用VisualStudio附带的一个项目模板开始,而不是像您所说的那样复制和粘贴随机的代码。这可以确保您拥有开始所需的所有东西,比如入口点。一旦你有了坚实的基础,你就可以从那里开始建立。

您可能还发现,获得一本关于C++/CLI的书或一本类似于以下内容的在线教程也很有用:和

您的测试函数是Form1类的成员函数,这意味着您需要该类的对象才能调用它。因此,代码实际上应该如下所示:

int main()
{
    Form1^ frm = gcnew Form1();
    frm.Test();

    return 0;
}
或者,您可以通过将测试函数设置为静态函数来解决此问题。这将允许您在没有类实例的情况下调用它:

public ref class Form1 : public System::Windows::Forms::Form
{
    public: static void Test(void)
    {
        // Does something
    }
}

// ...

int main()
{
    Form1::Test();

    return 0;
}
但是,请注意,这意味着您无法访问测试函数中Form1类的任何其他成员,因为没有此指针

这一切都应该在您决定用来学习C++/CLI的任何一本书/教程中进行解释,搜索有关类或面向对象设计的章节