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

Winforms 非静态成员引用必须相对于特定对象,winforms,variables,textbox,c++-cli,Winforms,Variables,Textbox,C++ Cli,我试图通过以下操作获取文本框tb_key中的文本,以写入我的std::string keyVariable: std::string Key=triprecloware::LoginForm::tb_Key->Text; 我得到一个错误,说: 非静态成员引用必须相对于特定的 反对 我试着搜索,但找不到任何能帮我解决问题的东西 最小可重复示例: LoginForm.h namespace-triprecloware{ 使用名称空间系统; 使用名称空间System::ComponentModel;

我试图通过以下操作获取文本框
tb_key
中的文本,以写入我的
std::string key
Variable

std::string Key=triprecloware::LoginForm::tb_Key->Text;
我得到一个错误,说:

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

我试着搜索,但找不到任何能帮我解决问题的东西

最小可重复示例:

LoginForm.h

namespace-triprecloware{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
private:System::Void tb\u key\u TextChanged(System::Object^sender,System::EventArgs^e)
{
}
}
LoginForm.cpp

std::string Key=triprecloware::LoginForm::tb_Key->Text;
我正试图在LoginForm.h中使用它

private:System::Void按钮1\u单击(系统::对象^sender,系统::事件参数^e){
if(Authenticate(StringToChar(Key),(StringToChar(hwid))//验证Key&hwid
{
这个->隐藏();
软件^soft=gcnew Software();
软->显示();
}
基本上,我想从名为
tb\u Key
的文本框中获取密钥并编写 将其添加到上面定义的my
变量。然后使用该键 验证并执行
code


你真正的问题是重复的

更正代码:

#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();
    }
}
#包括
#包括
使用名称空间系统;
使用名称空间msclr::interop;
无效按钮1\u单击(系统::对象^sender,系统::事件参数^e)
{
std::string Key=marshal_as(tb_Key->Text);
if(Authenticate(Key.c_str(),hwid.c_str())//验证Key&hwid
{
隐藏();
软件^soft=gcnew Software();
软->显示();
}
}

你的意思是
String^sKey=this->tb_key->Text;
?假设LoginForm是当前的类。但是你也可以使用
MyNameSpace::LoginForm::tb_key->Text
表单。使用
MyNameSpace::LoginForm::tb_key->Text
会给出上面提到的错误。我在.cpp文件中定义变量,而不是在.h原始表单文件中。请显示一个。假定
LoginForm
是一个类而不是对象,那么您没有LoginForm的实例,因此
MyNameSpace::LoginForm
表示一个类型,而
tb\u key
是一个实例成员。您需要对已创建表单的实例进行引用。这是
MyNameSpace.cpp
文件吗?我重命名了它d MyNameSpace在上面的代码中是TripRecocilware,因此LoginForm是.h和.cpp文件。非常感谢!我收到了以下错误tho:Severity code Description Project file Line Suppression State error C4996'msclr::interop::error\u reporting\u helper::marshal\u as':此转换所需的库或头文件不支持此转换不包括版本。请参阅有关“如何:扩展封送处理库”的文档,以添加您自己的封送处理方法。TripRecoCloware C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\msclr\marshal.h216@TRAV:抱歉,我丢失了两个必需的头文件中的一个。错误消息完全正确“未包含此转换所需的头文件”。我现在已将其添加到我的答案中。此处可以找到转换所需的头文件列表:谢谢,修复了它!我现在面临其他错误LNK2028和LNK 2019严重性代码描述项目文件行抑制状态错误LNK2028未解析令牌(0A00050D)“外部”C“bool\u cdecl初始化(char const*)”(?初始化@@$$J0YA)_NPBD@Z)在函数“int\uu clrcall main(cli::array^)”中引用(?main@$$HYMHP$01AP$AAVString@System@@@Z) 它来自Initialize(“245T8RTZNEOGWSMSVOPW6UK7YW62DADWJQLWX935RDZIP2K1LEWJ4OP4EJHDF”);它需要常量字符