非静态成员引用必须相对于特定对象

问题描述

我正在尝试将文本框 tb_key 中的文本写入我的 std::string Key 变量 这样做:

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

我收到一条错误消息:

非静态成员引用必须相对于特定的 对象

我尝试搜索,但找不到任何真正适合我的东西。

最小可重复示例:

LoginForm.h

namespace TRIPRECOILWARE {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;



private: System::Void tb_key_TextChanged(System::Object^ sender,System::EventArgs^ e) 
{
}

}

LoginForm.cpp

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

我正在尝试在 LoginForm.h

中使用它
private: System::Void button1_Click(System::Object^ sender,System::EventArgs^ e) {
    
    if (Authenticate(Stringtochar(Key),(Stringtochar(hwid)))) // Authenticates key & hwid
    {
        this->Hide();
        Software^ soft = gcnew Software();
        soft->Show();
    }

基本上,我想从名为 tb_key 的 TextBox获取 Key 并写入 它到我上面定义的 Key 变量。然后使用该键 验证并执行 code

解决方法

你真正的问题是How to turn System::String^ into std::string?

的重复

更正的代码:

#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace msclr::interop;

void button1_Click(System::Object^ sender,System::EventArgs^ e)
{
    std::string Key = marshal_as<std::string>(tb_key->Text);
    if (Authenticate(Key.c_str(),hwid.c_str())) // Authenticates key & hwid
    {
        Hide();
        Software^ soft = gcnew Software();
        soft->Show();
    }
}