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

フォームのオーバーロードされたコンストラクターを作成できませんか。すなわち

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 に答える