VS2012 C++ で Windows フォーム アプリケーションを作成しています。たとえば、実際のプロジェクトはもっと複雑です。TextBox、Button、Timer を含む Form があります。ボタンはタイマーをトリガーするだけです。タイマーは、変数をインクリメントする関数を呼び出すだけです。インクリメントされる関数の変数を TextBox に表示する必要があります。
Form1.h にコードを追加します。
public: void Timer_Function(); //function activated by timer Tick
void Set_Text(String ^str); //function to set TextBox Text
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (timer1->Enabled == false) timer1->Enabled = true;
else timer1->Enabled = false;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Timer_Function();
}
My_app.cpp コードでは次のようになります。
#include "stdafx.h"
#include "Form1.h"
#include "resource.h"
using namespace test_staticfunc;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
void Form1::Timer_Function()
{
Timer_Func();
}
void Form1::Set_Text(String ^str)
{
textBox1->Text = str;
}
void Timer_Func()
{
static int I=0;
I++;
Form1::Set_Text(I.ToString());
}
関数 Timer_Func() は、次のように「resource.h」で指定されます。
void Timer_Func();
つまり、Timer_Func() の内部変数 I の現在の状態を Form1 パブリック メソッド Set_Text() に渡すことによって表示しようとしています。そう。ここでのエラーは、Set_Text() が静的メソッドではないことです。静的にしようとしたのですが、「С2227: The operand to the left of "->Text" is not a pointer to a class, structure, or union.」というエラーが出ました。それを正しくする方法は?その場合、静的メソッドは非静的メソッドを実装しようとしていますよね?
または別の方法: Form1 のインスタンスを作成する - 代わりに
Application::Run(gcnew Form1());
コードを挿入
Form1 ^My_form = gcnew Form1();
Application::Run(My_form);
また、クラス インスタンス My_form の非静的メソッドとして Set_Text を使用します。ただし、My_form は main() でのみ使用できます。My_form は他では作れませんでした。それをグローバルにする方法はありますか?
この問題を解決する他の方法があるかもしれませんか?
助けてください!すでにいくつかのフォーラムで回答を検索しましたが、回答が見つかりませんでした。より正確には、それらのどれも適していません。PS私の悪い英語でごめんなさい!^_^