I am trying to run older VC++ code in 2010VC++ I am getting error C387 error
this->InitButton->Location = System::Drawing::Point(24, 8);
this->InitButton->Name = S"InitButton";
this->InitButton->Size = System::Drawing::Size(184, 24);
this->InitButton->TabIndex = 0;
this->InitButton->Text = S"Initialize NMC Network";开发者_JS百科
// this give an error
this->InitButton->Click += new System::EventHandler(this, InitButton_Click);
this->InitButton->Click += gcnew System::EventHandler(this, InitButton_Click);
That's C3867, not C387. You have to specify the class name with the method name in C++/CLI. It's syntax for assigning delegates is quite unlike the one in the C# language, there's no syntax sugar at all. This isn't otherwise associated with VS2010, it fails to compile in earlier editions too. Fix:
this->InitButton->Click += gcnew System::EventHandler(this, &Form1::InitButton_Click);
Replace Form1 with the name of your Form derived class. You don't actually need & but it is boilerplate in the designer generated code. Letting the designer generate this code is the best way to keep out of trouble.
In addition to Hans's answer concerning the correct way to get a pointer-to-member for delegate construction, C++/CLI does not use the S prefix on managed strings.
Thanks, You are right about the error code (typo). I was able to compile is with warning now. I made some changes. Changed common language run time support to old syntax and was able to compile and run it.
Common Language Runtime Support, Old Syntax (/clr:oldSyntax)
精彩评论