Visual C++ Windows 窗体中的 C2227 和 C3867 错误

C2227 and C3867 errors in visual c++ Windows Form

我正在尝试读取我的应用程序中的文件,但弹出一些我不完全理解的错误。

C2227: left of '->Equals' must point to class/struct/union/generic type C3867: 'System::String:ToString': non-standard syntax; use '&' to create a pointer to member

代码如下:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    OpenFileDialog^ open = gcnew OpenFileDialog();

    open->InitialDirectory = "c:\";
    open->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    open->FilterIndex = 2;
    open->RestoreDirectory = true;

    if (open->ShowDialog->Equals(System::Windows::Forms::DialogResult::OK) )
    {
        textBox1->Text = open->FileName;
    }
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    ifstream inFile;

    string time;
    string name;
    Shack shackPref;
    pair<int, int> av;
    string filename = textBox1->Text->ToString;

    inFile.open(filename);

    if (inFile.is_open()) {
        cout << "File has been opened" << endl;
    }
    else {
        cout << "NO FILE OPENED" << endl;
    }

    while (!inFile.eof()) {

    }

有人可以解释为什么 open->ShowDialog 不属于 class/struct/union/generic 类型吗?我如何重新排列它以使其成为正确的输入?我是否需要创建一个保存该数据的结构,如果需要,'->Equals' 是否仍然能够将它与 DialogResult 进行比较?

尝试将文件名从 String^ 转换为 std:string 时也会遇到同样的问题。当我尝试将 Text 视为 textBox1 的成员时,它说它没有该成员。为什么“->ToString”不能将 'Text' 识别为成员?

提前感谢您的帮助。我对 c++ 还是很陌生,所以这对我来说真是一个大脑问题。

您正在尝试将 ShowDialog 用作 属性 或成员,但它是一种方法,因此需要使用括号调用它。

open->ShowDialog()

ToString也是如此。它需要被调用,因为它是一个函数。

textBox1->Text.ToString();

但除此之外,对 String 调用 ToString() 毫无意义。您应该能够使用此问题的答案将 .net 字符串转换为 std::string.

C++ .NET convert System::String to std::string