2016-05-31 46 views
-2

你好我想在Visual Studio 2013上使用Windows应用程序窗体做一个C++登录界面。问题是,我试图比较文本框中的值与文件中的行,但我出现操作数类型不兼容的错误。使用Windows应用程序的C++登录界面

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 
     string un, pw; 

     ifstream read("file.txt"); 
     getline(read, un); 
     getline(read, pw); 


     if (textBox1->Text = un && textBox2->Text = pw){ 

      MessageBox::Show("You have successfully login!", "Login Message", MessageBoxButtons::OK, MessageBoxIcon::Information); 

     } 

     else { 
      MessageBox::Show("Incorrect Password or Username !", "Login Message", MessageBoxButtons::OK, MessageBoxIcon::Error); 
        } 

     read.close(); 


    } 
+2

如果你不擅长C++并且不想学习这门语言,为什么要在其中编写代码? – SergeyA

+2

如果你有编译错误,你应该分享它们。发生错误是不好的。 – NathanOliver

+0

@NathanOliver,想分享一下我的? :) – SergeyA

回答

0

两者是错在这里:

if (textBox1->Text = un && textBox2->Text = pw){ 

问题1:unstd::string,a C++ standard library stringtextBox1->TextSystem::String ^,指向a .Net string的托管指针。

这些类型是非常不同的,并不是(自动)可比较的。你需要将一个转换为另一个来比较它们。鉴于将System::String转换为std::string比其他方式通常要烦恼得多,因为System::String是一个局部化的,基于字符的宽字符串,让我们沿着阻力最小的路径走。

if (textBox1->Text = gcnew String(un.c_str()) && textBox2->Text = gcnew String(pw.c_str())){ 

现在值是相同的类型,System::String。这暴露了问题2.

问题2:=是赋值运算符。这目前试图分配untextBox1->Text,而不是比较。你的意思是写:

if (textBox1->Text == gcnew String(un.c_str()) && textBox2->Text == gcnew String(pw.c_str())){ 
0

在C++中,您的赋值运算符是'=',您的比较运算符是'=='。

你会想你的代码更改为:如果(textBox1->文本==未& & textBox2->文本== PW)

+0

错误仍然是一样的。哪些是操作数类型不兼容(“System :: String ^”和“std :: string”) – Lily

+0

这是因为它们*是*不同类型。 [你在混合使用C++和CLI](https://en.wikipedia.org/wiki/C%2B%2B/CLI)数据类型。这个答案可能会有所帮助:http://stackoverflow.com/questions/13718188/convert-from-stdstring-to-string或直接从马的嘴巴:https://msdn.microsoft.com/en-us/library/ms235219 .aspx – user4581301

+0

谢谢!它帮助! – Lily