当我尝试在代码下面运行时,我是编程新手(我只粘贴了必要的部分)我收到了错误错误: C2065:' myfilename' :未声明的标识符
#pragma once
#include <string>
namespace First_App {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MySql::Data::MySqlClient;
using namespace System::IO;
using namespace std;
// other events while clicking buttons etc .
private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) {
Stream^ myStream;
OpenFileDialog^ OpenFiledialogVariable1 = gcnew OpenFileDialog();
if (OpenFiledialogVariable1->ShowDialog()==System::Windows::Forms::DialogResult::OK)
{
if ((myStream = OpenFiledialogVariable1->OpenFile()) != nullptr)
String^ myfilename = OpenFiledialogVariable1->InitialDirectory + OpenFiledialogVariable1->FileName;
MessageBox::Show(myfilename); // ERROR shown in this line
myStream->Close();
}
}
我试过搜索msdn docs,谷歌没有找到任何相关内容。我想我确实为字符串类包含了必需的头文件,但仍然无法正常工作。感谢任何帮助。
答案 0 :(得分:1)
你只是缺少括号:
if ((myStream = OpenFiledialogVariable1->OpenFile()) != nullptr) { // added
String^ myfilename = OpenFiledialogVariable1->InitialDirectory + OpenFiledialogVariable1->FileName;
MessageBox::Show(myfilename); // ERROR shown in this line
myStream->Close();
} // added
由于您没有大括号,因此myFilename声明对于其下的语句不可见,该语句位于不同的(外部)范围内。