2

我已经学习 C++/CLI 几个月了,但无论我尝试什么,我似乎都无法解决我遇到的问题。

我需要将 String^、Array^ 或 ArrayList^ 数据类型从 main.cpp 传递到 Form1.h。我试图在 main.cpp 中使变量成为全局变量,然后使用 extern 调用该变量。但是,这不适用于 String、Array 和 ArrayList 数据类型。

我该怎么做呢?提前致谢。这是我的代码的解释:

//main.cpp

bool LoadFileFromArgument = false; //A boolean can be declared global
String^ argument; //this will not pass from main.cpp to Form1.h

int main(array<System::String ^> ^args)
{
String ^ argument = args[1]

// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it
Application::Run(gcnew Form1());

return 0;
}



//Form1.h

public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //

        extern bool LoadFileFromArgument; //is grabbed without error
        extern String^ argument; //will not work

这是错误:

error C3145: 'argument' : global or static variable may not 
have managed type 'System::String ^'

may not declare a global or static variable,
or a member of a native type that refers to objects in the gc heap
4

1 回答 1

2

你能不能为表单创建一个重载的构造函数。IE

public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(String^ argument)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        // 
        // Use "argument" parameter as req'd.
    }

    Form1(void)
    {
        //....usual constructor here...
        //..etc...

然后从主要

// Create the main window and run it
Application::Run(gcnew Form1(argument));
于 2012-11-12T10:19:50.550 回答