问题描述
我已经为这个错误苦苦挣扎了一段时间了。
我正在尝试实现到ATM系统的登录,也试图将其连接到数据库。但是当我尝试运行它时,会显示144个相同的错误:
'MyForm2': is not a member of 'jiji'
'MyForm2': undeclared identifier
Syntax error: missing ';' before identifier 'f2'
'f2':undeclared identifier
老实说,我不知道为什么会这样。我试图在主代码中包含MyForm2.h
,但没有任何变化。
在这里,我保留了所有7种形式的代码几乎相同的代码(除了第二种代码,它给出了我认为的错误)
MyForm.h:
private: System::Void button1_Click(System::Object^ sender,System::EventArgs^ e) {
try {
String^ User = textBox1->Text;
String^ Pwd = textBox2->Text;
int AN = Int32::Parse(textBox3->Text);
String^ constr = "Server=127.0.0.1;Uid=root;Pwd=;Database=visualcppdb";
MySqlConnection^ con = gcnew MySqlConnection(constr);
MySqlDataAdapter^ da = gcnew MySqlDataAdapter("Select AccountNum from Userinfo WHERE AccountNum = '" + AN + "'",con);
DataTable^ dt = gcnew DataTable();
da->Fill(dt);
if (dt->Rows->Count >= 1) {
MySqlCommand^ cmd = gcnew MySqlCommand("insert into Db values (" + AN + ") ",con);
con->Open();
MySqlDataReader^ dr = cmd->ExecuteReader();
con->Close();
try {
this->Hide();
jiji::MyForm2 f2;
f2.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
else {
MessageBox::Show("User does not exist,try again");
}
}
catch (Exception^ e) {
MessageBox::Show(e->Message);
}
}
顺便说一下,AN是帐号。
MyForm2.h代码一遍又一遍,除了结尾。
MyForm2.h:
//Extract money
private: System::Void button1_Click(System::Object^ sender,System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm3 f3;
f3.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Insert Money
private: System::Void button2_Click(System::Object^ sender,System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm4 f4;
f4.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Transfer
private: System::Void button3_Click(System::Object^ sender,System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm5 f5;
f5.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Check Amount
private: System::Void button4_Click(System::Object^ sender,System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm6 f6;
f6.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Reload phone
private: System::Void button5_Click(System::Object^ sender,System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm7 f7;
f7.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Exit
private: System::Void button6_Click(System::Object^ sender,System::EventArgs^ e) {
try {
String^ constr = "Server=127.0.0.1;Uid=root;Pwd=;Database=visualcppdb";
MySqlConnection^ con = gcnew MySqlConnection(constr);
MySqlCommand^ cmd = gcnew MySqlCommand("TRUNCATE Db ",con);
con->Open();
MySqlDataReader^ dr = cmd->ExecuteReader();
con->Close();
}
catch (Exception^ e) {
MessageBox::Show(e->Message);
}
finally {
this->Close();
}
}
解决方法
也许您没有在MyForm2.h
的上下文中包括MyForm.h
吗?
也许要研究如何声明名称空间以及如何包含标头,例如Creating a C++ namespace in header and source (cpp)
如果将MyForm2
类定义内联到MyForm
中以确保可以访问,会发生什么?